<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/15">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Redis/9. Redis基本操作——String/">Redis基本操作——String</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关于Redis的命令，主要参考<a href="http://redis.io/commands#string" target="_blank" rel="external">Redis的官方文档</a>。这里主要像是翻译一下，并且给出一些实用的例子。排版上，按功能重新组织了一下指令，并将比较常用的指令放在了最前面。这里的Redis版本是3.0.7，应该是目前的最新版。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：redis的命令（SET，GET等）是不区分大小写的（KEY和VALUE区分的），为了方便所以操作可能都是小写的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指令清单:</p>
<h2 id="一，SET的相关操作"><a href="#一，SET的相关操作" class="headerlink" title="一，SET的相关操作"></a>一，SET的相关操作</h2><h3 id="1-SET-key-value-EX-seconds-PX-milliseconds-NX-XX"><a href="#1-SET-key-value-EX-seconds-PX-milliseconds-NX-XX" class="headerlink" title="1.SET key value [EX seconds] [PX milliseconds] [NX|XX]"></a>1.SET key value [EX seconds] [PX milliseconds] [NX|XX]</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Set the string value of a key</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将键key设置成指定的字符串value。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们知道Redis是一种key-value数据库（当然这么说可能不够严谨，因为Redis支持很多高级的数据结构）。因此所有的数据都是通过key来访问，key就是键。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SET指令可以将字符串的value和key绑定在一起。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果不加额外的参数。当key中已经保存了一个值的话，这个值会被覆盖成新的值，并且忽略掉原始类型（一个key对应的不一定是一个字符串，也可以是List等其他的数据结构）；如果key不存在，那么则会在数据库中新增一个key，对应的值就是你刚刚设置的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例子：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">redis&gt; get name</div><div class="line">(nil)</div><div class="line">redis&gt; <span class="built_in">set</span> name miaoerduo</div><div class="line">OK</div><div class="line">redis&gt; get name</div><div class="line"><span class="string">"miaoerduo"</span></div><div class="line">redis&gt; <span class="built_in">set</span> name miao</div><div class="line">OK</div><div class="line">redis&gt; get name</div><div class="line"><span class="string">"miao"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;GET命令用于查看key对应的值，我们下面会讲到。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第一个get，由于name这个key不存在，因此我们得到的结果是nil，也就是说空。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;之后我们设置一次，然后就get到我们的结果了。之后再设置一次，会发现结果已经被覆盖。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这就是set命令的基本用法。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们看到文档的后面有一些可选的参数。下面依次来介绍：</p>
<ul>
<li><strong>EX seconds</strong>：设置key的过时时间，单位为秒。</li>
<li><strong>PX milliseconds</strong>：设置key的过期时间，单位为毫秒。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以ex为例：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis&gt; <span class="built_in">set</span> name miaoerduo ex 10</div><div class="line">OK</div><div class="line">redis&gt; get name</div><div class="line"><span class="string">"miaoerduo"</span></div><div class="line">redis&gt; get name</div><div class="line">(nil)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们设置了name的过期时间为10秒。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在设置完name之后，立刻获取name的值，可以得到正确的输出。但是过了10s之后，发现只能得到一个nil。说明这个key已经被清空了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;那么这个设置有什么作用呢？</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们说几个常见的应用场景：</p>
<ul>
<li><strong>验证码</strong></li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们经常在登录一些网站或是进行付款等操作的时候，都会收到一些验证码，并且说10min后失效。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;实际上就可以通过下面一条指令来实现：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">set</span> phone_num code ex 600</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用手机号作为key，验证码作为值，超时6min。这样当你输入好验证码，提交的时候，后台就可以了先get phone_num，再比较你的输入和数据库里面存的值，从而完成身份的验证。</p>
<ul>
<li><strong>session</strong></li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;早前，网站通过cookie来保存用户的用户名和密码，之后出现了很多的安全隐患，因此就提出了session的机制。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用户登陆成功之后，本地的cookie会保留一个较长的随机码，而网站后的后台则存储了这个随机码和你的用户id的对应关系。在你第二次登录的时候，cookie会传输到后台，而后台则根据你的随机码，获取你的用户信息，如果符合，则自动登录。这样，即使网站上有不法分子获取了你的cookie，也得不到你的任何信息，因为你的真实的有用的信息都存储在网站的后台。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们在登录邮箱的时候，通常都会有一个选项，7天内自动登录。这其实就是给后台存的session设置了一个超时。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>NX</strong>：（if Not eXist）只有键key不存在的时候才会设置key的值</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>XX</strong>：只有键key存在的时候才会设置key的值</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;NX通常用于实现锁机制，XX的功能，小喵暂时木有头绪。。。想到应用场景的筒子们可以留言告诉小喵，小喵看到了一定会更新的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;举个例子：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">redis&gt; del name</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis&gt; get name</div><div class="line">(nil)</div><div class="line">redis&gt; <span class="built_in">set</span> name miaoerduo nx</div><div class="line">OK</div><div class="line">redis&gt; <span class="built_in">set</span> name miaoerduo nx</div><div class="line">(nil)</div><div class="line">redis&gt; get name</div><div class="line"><span class="string">"miaoerduo"</span></div><div class="line">redis&gt; <span class="built_in">set</span> name miao xx</div><div class="line">OK</div><div class="line">redis&gt; get name</div><div class="line"><span class="string">"miao"</span></div><div class="line">redis&gt; del name</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis&gt; <span class="built_in">set</span> name miao xx</div><div class="line">(nil)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先我们使用del指令删除了name（这个指令后面会介绍）。之后使用nx模式设置name，第一次成功了，而第二次就失败了，因为name已经存在了。之后使用xx模式修改，发现确实可以。del name之后，再使用xx模式，就失败了，因为此时的name已经不存在了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上，就是set的所有的用法。</p>
<h3 id="2-SETRANGE-key-offset-value"><a href="#2-SETRANGE-key-offset-value" class="headerlink" title="2.SETRANGE key offset value"></a>2.SETRANGE key offset value</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Overwrite part of a string at key starting at the specified offset</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个命令可以覆盖掉key对应的string的一部分。offset表示需要覆盖的字符串的起始位置，之后会用value的值，覆盖掉原始string的对应位置的数据。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里有一些比较有意思的操作：如果原始key不存在，则默认为一个长度为0的字符串。如果offset超过原始string的长度，那么会在之前的string后面补充0以达到offset。如果value的长度超过了原始string后面可以覆盖的部分，则Redis内部会重新申请内存，完成数据的追加（还记得上一章的内容吗？），这时候数据库由于需要分配内存，可能会出现阻塞（需要分配的内存越大，阻塞时间越长）。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例子：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">redis&gt; <span class="built_in">set</span> str <span class="string">"hello world"</span></div><div class="line">OK</div><div class="line">redis&gt; setrange str 6 redis</div><div class="line">(<span class="built_in">integer</span>) 11</div><div class="line">redis&gt; get str</div><div class="line"><span class="string">"hello redis"</span></div><div class="line">redis&gt; setrange str 15 aha</div><div class="line">(<span class="built_in">integer</span>) 18</div><div class="line">redis&gt; get str</div><div class="line"><span class="string">"hello redis\x00\x00\x00\x00aha"</span></div><div class="line">redis&gt; del str</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis&gt; setrange str 5 <span class="string">"hello miao"</span></div><div class="line">(<span class="built_in">integer</span>) 15</div><div class="line">redis&gt; get str</div><div class="line"><span class="string">"\x00\x00\x00\x00\x00hello miao"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在这个实验中，我们先新建了一个str，内容是”hello world”，之后，从位置6开始写入字符串”redis”，则得到了”hello redis”。之后我们在15的位置，写入”aha”，这是offset已经比字符串的长度要大了，则Redis会默认填充0（\x00是0的16进制表达），之后再追加字符串。最后，我们给一个不存在的key使用setrange设置了一个值，结果表现得和空字符串一样。</p>
<h3 id="3-MSET-key-value-key-value-…"><a href="#3-MSET-key-value-key-value-…" class="headerlink" title="3.MSET key value [key value …]"></a>3.MSET key value [key value …]</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Set multiple keys to multiple values</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一次性设置多个key-value。如果key的值已存在，则会直接覆盖。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;相当与同时调用多次SET命令。不过要注意的是，这个操作是原子的。也就是说，所有的keys都一次性设置的。如果你同时运行两个MSET来设置相同的keys，那么操作的结果也只会是两次MSET中的后一次的结果，而不会是混杂的结果。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例子：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">redis&gt; mset name1 miaoerduo name2 miao name3 love</div><div class="line">OK</div><div class="line">redis&gt; get name1</div><div class="line"><span class="string">"miaoerduo"</span></div><div class="line">redis&gt; get name2</div><div class="line"><span class="string">"miao"</span></div><div class="line">redis&gt; get name3</div><div class="line"><span class="string">"love"</span></div></pre></td></tr></table></figure>
<h3 id="4-MSETNX-key-value-key-value-…"><a href="#4-MSETNX-key-value-key-value-…" class="headerlink" title="4.MSETNX key value [key value …]"></a>4.MSETNX key value [key value …]</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Set multiple keys to multiple values, only if none of the keys exist</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一次性设置多个key-value。如果存在任何一个key已经存在，那么这个操作都不会执行。所以，当使用MSETNX的时候，要么全部key都被修改，要么全部都不被修改。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当然这个操作也是原子的。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">redis&gt; get name3</div><div class="line"><span class="string">"love"</span></div><div class="line">redis&gt; msetnx name3 miaoerduo name4 miao</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis&gt; get name4</div><div class="line">(nil)</div><div class="line">redis&gt; msetnx name4 miaoerduo name5 miao</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis&gt; get name4</div><div class="line"><span class="string">"miaoerduo"</span></div><div class="line">redis&gt; get name5</div><div class="line"><span class="string">"miao"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当name3已经存在的时候，我们设置name3和name4，发现，连name4都没有创建。当设置name4和name5的时候，由于这两个key之前都不存在，因此设置成功了。</p>
<h3 id="5-SETEX-key-seconds-value"><a href="#5-SETEX-key-seconds-value" class="headerlink" title="5.SETEX key seconds value"></a>5.SETEX key seconds value</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Set the value and expiration of a key</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置key的值和超时。和前面的set key value ex seconds一样。</p>
<h3 id="6-PSETEX-key-milliseconds-value"><a href="#6-PSETEX-key-milliseconds-value" class="headerlink" title="6.PSETEX key milliseconds value"></a>6.PSETEX key milliseconds value</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Set the value and expiration in milliseconds of a key</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置key的值和超时。和前面的set key value px milliseconds一样。</p>
<h3 id="7-SETNX-key-value"><a href="#7-SETNX-key-value" class="headerlink" title="7.SETNX key value"></a>7.SETNX key value</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Set the value of a key, only if the key does not exist</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当key不存在的时候，设置key的值。和set key value nx一样。</p>
<h2 id="二、GET的相关操作"><a href="#二、GET的相关操作" class="headerlink" title="二、GET的相关操作"></a>二、GET的相关操作</h2><h3 id="1-GET-key"><a href="#1-GET-key" class="headerlink" title="1.GET key"></a>1.GET key</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Get the value of a key</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;根据给定的key，获取value值。这个操作，我们之前已经使用了很多次。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果key不存在，会返回nil。如果key对应的值不是string(List，Set等)，则会报错，因为GET只能处理string类型的value。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;演示如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">redis&gt; get name</div><div class="line">(nil)</div><div class="line">redis&gt; <span class="built_in">set</span> name <span class="string">"miaoerduo"</span></div><div class="line">OK</div><div class="line">redis&gt; get name</div><div class="line"><span class="string">"miaoerduo"</span></div><div class="line">redis&gt; lpush arr 1 2 3 4 5</div><div class="line">(<span class="built_in">integer</span>) 5</div><div class="line">redis&gt; get arr</div><div class="line">(error) WRONGTYPE Operation against a key holding the wrong kind of value</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意，lpush是List部分的内容，arr的值是一个List的结构。这里只需要知道key不是  string类型的时候，get的时候就会报错。</p>
<h3 id="2-MGET-key-key-…"><a href="#2-MGET-key-key-…" class="headerlink" title="2.MGET key [key …]"></a>2.MGET key [key …]</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Get the values of all the given keys</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还记得我们之前可以同时设置多个key-value吗？其实我们也可以一次性获取多个key的值。如果key不存在，则对应的地方返回nil。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;那么一次性获取多个值和单独一次一次GET有什么区别呢？</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主要有两点，原子操作和查询效率。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;比如博客上会统计评论数和访问数。如果我们依次读取这两部分的值，那么可能得到的两个值并不是同一时刻的。而如果使用MGET，则得到的一定是同一时刻的。这就是原子操作的威力（注，官方文档只介绍了SET和GET的操作是原子的，并没有说MGET是不是，这里小喵果断的说MGET是原子操作也是不合理的。但考虑到使用MGET的时候，是把查询指令一次性传输到后台来执行，所以应该原子操作。无论如何，在上面的例子中，使用MGET总是比两次GET要合理的）。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另外，如果数据库的查询，都分为三个过程，传输查询指令，执行指令，输出结果。<br>如果分多次GET的话，在传输指令和输出结果的这两个部分就要重复很多次，效率大打折扣。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;分多次查询，需要传输的指令数目，也不会比单次多很多，为什么一定会影响效率呢？</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其实不然，传输数据需要很多的准备工作，不仅仅是数据的具体传输，有时候需要考虑连接的创建和关闭、设置锁等的开销。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">redis&gt; <span class="built_in">set</span> key1 a</div><div class="line">OK</div><div class="line">redis&gt; <span class="built_in">set</span> key2 b</div><div class="line">OK</div><div class="line">redis&gt; mget key1 key2 key3</div><div class="line">1) <span class="string">"a"</span></div><div class="line">2) <span class="string">"b"</span></div><div class="line">3) (nil)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们只创建了key1和key2，使用MGET获取的时候，由于没有key3，对应的位置返回了nil。</p>
<h3 id="3-GETRANGE-key-start-end"><a href="#3-GETRANGE-key-start-end" class="headerlink" title="3.GETRANGE key start end"></a>3.GETRANGE key start end</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Get a substring of the string stored at a key</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该指令只要用于获取字符串的字串，在Redis2.0版本之前，叫做SUBSTR。strat和end是字串的起始和结束的位置，可以用负数来表示距离string尾部的未知的下标。-1是最后一个字符，-2是底数第二个字符。这个表达方式和Python的获取list的子list非常相似。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;需要注意的有两点：</p>
<ul>
<li>字串包括了start和end这两个位置的字符。在Python中是不包含end的。</li>
<li>当给出的start和end超出了string的范围时，指令只会返回在string内的结果。</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">redis&gt; <span class="built_in">set</span> str <span class="string">"hello world"</span></div><div class="line">OK</div><div class="line">redis&gt; getrange str 6 -1</div><div class="line"><span class="string">"world"</span></div><div class="line">redis&gt; getrange str -5 -1</div><div class="line"><span class="string">"world"</span></div><div class="line">redis&gt; getrange str 0 4</div><div class="line"><span class="string">"hello"</span></div><div class="line">redis&gt; getrange str 0 100</div><div class="line"><span class="string">"hello world"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上述例子是几种情况下的输出。</p>
<h3 id="4-GETSET-key-value"><a href="#4-GETSET-key-value" class="headerlink" title="4.GETSET key value"></a>4.GETSET key value</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Set the string value of a key and return its old value</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置key的值，并返回之前的值。如果之前key的值不是string，则会报错。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个指令相当于先GET，再SET。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个指令可以用来配合INCR指令一起使用支持重置的技术功能（INCR我们后面会讲到）。先设置count为0，每次INCR使得count加1。等到需要获取计数的时候，使用GETSET count 0，就能获取计数的值，并且把计数器重置了。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis&gt; <span class="built_in">set</span> str <span class="string">"hello"</span></div><div class="line">OK</div><div class="line">redis&gt; getset str <span class="string">"world"</span></div><div class="line"><span class="string">"hello"</span></div><div class="line">redis&gt; get str</div><div class="line"><span class="string">"world"</span></div></pre></td></tr></table></figure>
<ul>
<li>第一行设置str为”hello”</li>
<li>第二行获取了str原先的值，并把str设置成”world”。</li>
<li>第三行GET的时候，就是修改之后的值了。</li>
</ul>
<h2 id="三、string的修改操作"><a href="#三、string的修改操作" class="headerlink" title="三、string的修改操作"></a>三、string的修改操作</h2><h3 id="1-STRLEN-key"><a href="#1-STRLEN-key" class="headerlink" title="1.STRLEN key"></a>1.STRLEN key</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Get the length of the value stored in a key</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回key对应的string的长度，如果key对应的不是string，则报错。如果key不存在，则返回0（还是把key对应的看成空字符串）。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">redis&gt; <span class="built_in">set</span> str <span class="string">"hello world"</span></div><div class="line">OK</div><div class="line">redis&gt; strlen str</div><div class="line">(<span class="built_in">integer</span>) 11</div><div class="line">redis&gt; get nokey</div><div class="line">(nil)</div><div class="line">redis&gt; strlen nokey</div><div class="line">(<span class="built_in">integer</span>) 0</div></pre></td></tr></table></figure>
<h3 id="2-APPEND-key-value"><a href="#2-APPEND-key-value" class="headerlink" title="2.APPEND key value"></a>2.APPEND key value</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Append a value to a key</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果key已经存在，且值为string，则将value追加到值的后面，如果key不存在，则会创建一个空的字符串的key，然后执行追加操作。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">redis&gt; <span class="built_in">set</span> str <span class="string">"hello"</span></div><div class="line">OK</div><div class="line">redis&gt; append str <span class="string">" world"</span></div><div class="line">(<span class="built_in">integer</span>) 11</div><div class="line">redis&gt; get str</div><div class="line"><span class="string">"hello world"</span></div><div class="line">redis&gt; del str</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis&gt; append str <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 5</div><div class="line">redis&gt; get str</div><div class="line"><span class="string">"hello"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个例子中，我们先向已有值的str中append了一个字符串。然后向不存在的key，也添加了字符串。</p>
<h3 id="3-INCR-key"><a href="#3-INCR-key" class="headerlink" title="3.INCR key"></a>3.INCR key</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Increment the integer value of a key by one</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对存储在key的整数值进行原子的加1操作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果key不存在，则会设置默认值0，再加1。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果key存在，但是存储的值不是字符串，或者存储的字符串不能表示整数，则执行该操作时，会报错。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个操作仅限于64位的有符号的整型数据。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;比较有意思的是，虽然这个key存储的值是个字符串，但是该操作的效果和对相应的数字进行操作一样。而且，Redis中，在存储这类字符串的时候，底层上其实存储的就是一个整数，因此不存在存储上的浪费。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">redis&gt; <span class="built_in">set</span> count 0</div><div class="line">OK</div><div class="line">redis&gt; incr count</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis&gt; incr count</div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis&gt; del count</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis&gt; get count</div><div class="line">(nil)</div><div class="line">redis&gt; incr count</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis&gt; incr count</div><div class="line">(<span class="built_in">integer</span>) 2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;值得注意的是，该操作是原子操作，即使有多个请求传输到Redis，count执行的结果都不会错误，所以我们可以大胆放心的用这个功能实现多线程的计数功能。</p>
<h3 id="4-DECR-key"><a href="#4-DECR-key" class="headerlink" title="4.DECR key"></a>4.DECR key</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Decrement the integer value of a key by one</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对存储在key的整数值进行原子的减1操作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意事项和INCR一样。</p>
<h3 id="5-INCRBY-key-increment"><a href="#5-INCRBY-key-increment" class="headerlink" title="5.INCRBY key increment"></a>5.INCRBY key increment</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Increment the integer value of a key by the given amount</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对存储在key的整数值进行原子的加操作，加increment。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果key不存在，操作之前，key就会被置为0。如果key的value类型错误或者是个不能表示成数字的字符串，就返回错误。这个操作最多支持64位有符号的整型数字。基本上和INCR一样。</p>
<h3 id="6-DECRBY-key-decrement"><a href="#6-DECRBY-key-decrement" class="headerlink" title="6.DECRBY key decrement"></a>6.DECRBY key decrement</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Decrement the integer value of a key by the given number</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对存储在key的整数值进行原子的减操作，减increment。</p>
<h3 id="7-INCRBYFLOAT-key-increment"><a href="#7-INCRBYFLOAT-key-increment" class="headerlink" title="7.INCRBYFLOAT key increment"></a>7.INCRBYFLOAT key increment</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Increment the float value of a key by the given amount</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对存储造key中的浮点数进行原子的加操作，加increment。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果key不存在，操作之前，key就会被置为0。如果key的value类型错误或者是个不能表示成浮点数的字符串，就返回错误。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们并没有DECRBYFLOAT这个操作，因此想要实现减操作，只需要把increment设成负的就可以。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">redis&gt; <span class="built_in">set</span> a 1.5</div><div class="line">OK</div><div class="line">redis&gt; incrbyfloat a 10.1</div><div class="line"><span class="string">"11.6"</span></div><div class="line">redis&gt; incrbyfloat a 10.1</div><div class="line"><span class="string">"21.7"</span></div><div class="line">redis&gt; incrbyfloat a -10.1</div><div class="line"><span class="string">"11.6"</span></div><div class="line">redis&gt; incrbyfloat a -1.5e2</div><div class="line"><span class="string">"-138.39999999999999999"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;浮点数可以用一般的小数和科学计数法表示。</p>
<h2 id="四、二进制操作"><a href="#四、二进制操作" class="headerlink" title="四、二进制操作"></a>四、二进制操作</h2><h3 id="1-SETBIT-key-offset-value"><a href="#1-SETBIT-key-offset-value" class="headerlink" title="1.SETBIT key offset value"></a>1.SETBIT key offset value</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Sets or clears the bit at offset in the string value stored at key</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置或者清空key的value(字符串)在offset处的bit值。这里将string看成由bit组成的数组。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定位置的bit可以被设置，或者被清空，这个由value（只能是0或者1）来决定。当key不存在的时候，就创建一个新的字符串value。要确保这个字符串足够长到在offset处有bit值。参数offset需要大于等于0，并且小于2^32(限制bitmap大小为512)。当key对应的字符串增大的时候，新增的部分bit值都是设置为0。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该操作返回value原来的offset位置的bit值。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">redis&gt; setbit a 0 1</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis&gt; setbit a 1 1</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis&gt; setbit a 2 1</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis&gt; setbit a 3 1</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis&gt; get a</div><div class="line"><span class="string">"\xf0"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;a最开始不存在，使用setbit操作，将a的前4位都设置成1。最终就得到了\xf0，这是16进制表示的结果，前4位都是1，其他都是0。</p>
<h3 id="2-GETBIT-key-offset"><a href="#2-GETBIT-key-offset" class="headerlink" title="2.GETBIT key offset"></a>2.GETBIT key offset</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Returns the bit value at offset in the string value stored at key</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;获取key对应的string在offset处的bit值。当offset超出了字符串长度的时候，这个字符串就被假定为由0比特填充的连续空间。当key不存在的时候，它就认为是一个空字符串，所以offset总是超出范围，然后value也被认为是由0比特填充的连续空间。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">redis&gt; setbit a 7 1</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis&gt; getbit a 0</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis&gt; getbit a 7</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis&gt; getbit a 100</div><div class="line">(<span class="built_in">integer</span>) 0</div></pre></td></tr></table></figure>
<h3 id="3-BITCOUNT-key-start-end"><a href="#3-BITCOUNT-key-start-end" class="headerlink" title="3.BITCOUNT key [start end]"></a>3.BITCOUNT key [start end]</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Count set bits in a string</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;统计key的string的二进制中1的个数。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;start和end分别表示string的起始和结束位置，含义和GETRANGE中一样。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">redis&gt; setbit mykey 0 1</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis&gt; setbit mykey 10 1</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis&gt; setbit mykey 5 1</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis&gt; bitcount mykey</div><div class="line">(<span class="built_in">integer</span>) 3</div></pre></td></tr></table></figure>
<h3 id="4-BITOP-operation-destkey-key-key-…"><a href="#4-BITOP-operation-destkey-key-key-…" class="headerlink" title="4.BITOP operation destkey key [key …]"></a>4.BITOP operation destkey key [key …]</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Perform bitwise operations between strings</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对string进行bit级别的操作。具体操作有4种。AND，OR，XOR，NOT。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">BITOP AND destkey srckey1 srckey2 srckey3 … srckeyN</div><div class="line">BITOP OR destkey srckey1 srckey2 srckey3 … srckeyN</div><div class="line">BITOP XOR destkey srckey1 srckey2 srckey3 … srckeyN</div><div class="line">BITOP NOT destkey srckey</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;NOT操作后面只有一个目标key和srckey，是因为NOT操作是一元的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于AND，OR和XOR操作，Redis会将srckey1，srckey2，…，srckeyN这些字符串对位进行相关操作，之后将结果存入destkey中。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果srckey的length不相等的话，Redis内部会将短的字符串补齐，并填充上0。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">redis&gt; <span class="built_in">set</span> key1 <span class="string">"\xf0\xf0"</span></div><div class="line">OK</div><div class="line">redis&gt; <span class="built_in">set</span> key2 <span class="string">"\x0f\x0f"</span></div><div class="line">OK</div><div class="line">redis&gt; <span class="built_in">set</span> key3 <span class="string">"\xff\xff"</span></div><div class="line">OK</div><div class="line">redis&gt; bitop and destkey key1 key2 key3</div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis&gt; get destkey</div><div class="line"><span class="string">"\x00\x00"</span></div><div class="line">redis&gt; bitop or destkey key1 key2 key3</div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis&gt; get destkey</div><div class="line"><span class="string">"\xff\xff"</span></div><div class="line">redis&gt; bitop xor destkey key1 key2 key3</div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis&gt; get destkey</div><div class="line"><span class="string">"\x00\x00"</span></div><div class="line">redis&gt; bitop not destkey key1</div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis&gt; get destkey</div><div class="line"><span class="string">"\x0f\x0f"</span></div></pre></td></tr></table></figure>
<h3 id="5-BITPOS-key-bit-start-end"><a href="#5-BITPOS-key-bit-start-end" class="headerlink" title="5.BITPOS key bit [start] [end]"></a>5.BITPOS key bit [start] [end]</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Find first bit set or clear in a string</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回string的二进制中第一个0或1的位置。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里将string看成一个有许多bit组成的数组。其中start和end表示string的一个间隔，如果指定了start和end，则BITPOS只会查询这个区间。注意，start和end表示的字符的位置，不是bit的位置。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;即使指定了start和end，BITPOS这个操作也只会这个目标bit的绝对地址。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有几点需要注意：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在没有指定查询区间或只指定start的时候，查询bit位为0的位置时，如果string中没有该位，则会返回string的bit位的总数。比如在\xff\xff\xff直接查询bit为0的位置，Redis默认该字符串后面都是0，因此，返回的结果就是12（下标从0开始数的）。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果指定了查询区间，无论查询0或是1，在没查询到的时候只会返回-1。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在没有指定查询区间时，查询bit位为1的位置时，如果string中没有该位，则会返回-1，表示未查询到。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">redis&gt; <span class="built_in">set</span> bits <span class="string">"\x00\xff\x00\xff"</span></div><div class="line">OK</div><div class="line">redis&gt; strlen bits</div><div class="line">(<span class="built_in">integer</span>) 4</div><div class="line">redis&gt; bitpos bits 0</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis&gt; bitpos bits 1</div><div class="line">(<span class="built_in">integer</span>) 8</div><div class="line">redis&gt; bitpos bits 0 1 </div><div class="line">(<span class="built_in">integer</span>) 16</div><div class="line">redis&gt; bitpos bits 0 1 -1</div><div class="line">(<span class="built_in">integer</span>) 16</div><div class="line">redis&gt; bitpos bits 1 1 -1</div><div class="line">(<span class="built_in">integer</span>) 8</div><div class="line">redis&gt; bitpos bits 0 3</div><div class="line">(<span class="built_in">integer</span>) 32</div><div class="line">redis&gt; bitpos bits 0 3 3</div><div class="line">(<span class="built_in">integer</span>) -1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看上面的例子，bits的初始设置的二进制表示为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">00000000 11111111 00000000 11111111</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;直接获取0（bitpos bits 0）的位置为0，获取1（bitpos bits 1）的位置为8。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定开始位置为1（start = 1）的时候，0第一次出现（bitpos bits 1）的位置为 16。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定起始位置为3（start = 3）的时候，0第一次出现（bitpos bits 0 3）的位置为32。这是因为Redis在查询字符串的时候查询到了字符串的末尾，之后默认末尾的后面都是 0，因此得到了32这的个位置。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后，当指定必须在第3个字节（从0开始数）查询0的时候，由于查不到0，因此只返回了-1。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/8. Redis 起步/">Redis 起步</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;dis和JQuery一样是纯粹为应用而产生的，这里记录的是在CentOS 5.7上学习入门文章：</p>
<h2 id="1-Redis简介"><a href="#1-Redis简介" class="headerlink" title="1.Redis简介"></a>1.Redis简介</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis是一个key-value存储系统。和Memcached类似，但是解决了断电后数据完全丢失的情况，而且她支持更多无化的value类型，除了和string外，还支持lists（链表）、sets（集合）和zsets（有序集合）几种数据类型。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作，而且这些操作都是原子性的。</p>
<h2 id="2-Redis的性能"><a href="#2-Redis的性能" class="headerlink" title="2.Redis的性能"></a>2.Redis的性能</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面是官方的bench-mark数据：</p>
<ul>
<li>The test was done with 50 simultaneous clients performing 100000 requests.</li>
<li>The value SET and GET is a 256 bytes string.</li>
<li>The Linux box is running Linux 2.6, it’s Xeon X3320 2.5Ghz.</li>
<li>Text executed using the loopback interface (127.0.0.1).</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Results: <strong>about 110000 SETs per second, about 81000 GETs per second.</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更多详细数据请见官方bench-mark page（<a href="http://code.google.com/p/redis/wiki/Benchmarks）" target="_blank" rel="external">http://code.google.com/p/redis/wiki/Benchmarks）</a></p>
<h2 id="3-安装Redis"><a href="#3-安装Redis" class="headerlink" title="3.安装Redis"></a>3.安装Redis</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis的代码遵循ANSI-C编写，可以在所有POSIX系统（如Linux, *BSD, Mac OS X, Solaris等）上安装运行。而且Redis并不依赖任何非标准库，也没有编译参数必需添加。redis的安装出奇的简单，这可能也是他风靡的一个原因，让人很容易上手，不像某些东西，编译阶段就能让人完全绝望。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;先去官网下载源码：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget http://redis.googlecode.com/files/redis-2.4.6.tar.gz</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解压：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tar –zxvf redis-2.4.6.tar.gz</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编译</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;需要说明的事，redis的安装非常简单，已经有现成的Makefile文件，直接运行make命令即可。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">make</div><div class="line">make install</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis 由四个可执行文件：<strong>redis-benchmark、redis-cli、redis-server、redis-stat</strong> 这四个文件，加上一个<strong>redis.conf</strong>就构成了整个redis的最终可用包。它们的作用如下：</p>
<ul>
<li>redis-server：Redis服务器的daemon启动程序</li>
<li>redis-cli：Redis命令行操作工具。当然，你也可以用telnet根据其纯文本协议来操作</li>
<li>redis-benchmark：Redis性能测试工具，测试Redis在你的系统及你的配置下的读写性能</li>
<li>redis-stat：Redis状态检测工具，可以检测Redis当前状态参数及延迟状况</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;现在就可以启动redis了，redis只有一个启动参数，就是他的配置文件路径。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis-server /etc/redis.conf</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意，默认复制过去的<strong>redis.conf</strong>文件的<strong>daemonize</strong>参数为<strong>no</strong>，所以redis不会在后台运行，这时要测试，我们需要重新开一个终端。修改为<strong>yes</strong>则为后台运行redis。另外配置文件中规定了pid文件，log文件和数据文件的地址，如果有需要先修改，默认log信息定向到stdout.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面是redis.conf的主要配置参数的意义：</p>
<ul>
<li>daemonize：是否以后台daemon方式运行</li>
<li>pidfile：pid文件位置</li>
<li>port：监听的端口号</li>
<li>timeout：请求超时时间</li>
<li>loglevel：log信息级别</li>
<li>logfile：log文件位置</li>
<li>databases：开启数据库的数量</li>
<li>save <em> \</em>：保存快照的频率，第一个*表示多长时间，第三个*表示执行多少次写操作。在一定时间内执行一定数量的写操作时，自动保存快照。可设置多个条件。</li>
<li>rdbcompression：是否使用压缩</li>
<li>dbfilename：数据快照文件名（只是文件名，不包括目录）</li>
<li>dir：数据快照的保存目录（这个是目录）</li>
<li>appendonly：是否开启appendonlylog，开启的话每次写操作会记一条log，这会提高数据抗风险能力，但影响效率。</li>
<li>appendfsync：appendonlylog如何同步到磁盘（三个选项，分别是每次写都强制调用fsync、每秒启用一次fsync、不调用fsync等待系统自己同步）</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这时你可以打开一个终端进行测试了，配置文件中默认的监听端口是6379</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们可以开启一个Redis客户端进行测试</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@SNDA-192-168-1-114 ~]<span class="comment"># redis-cli </span></div><div class="line">Could not connect to Redis at 127.0.0.1:6379: Connection refused </div><div class="line">not connected&gt; <span class="built_in">exit</span> </div><div class="line">[root@SNDA-192-168-1-114 ~]<span class="comment"># redis-server /etc/redis.conf </span></div><div class="line">[root@SNDA-192-168-1-114 ~]<span class="comment"># redis-cli </span></div><div class="line">redis 127.0.0.1:6379&gt; quit</div></pre></td></tr></table></figure>
<h2 id="4-redis数据结构"><a href="#4-redis数据结构" class="headerlink" title="4.redis数据结构"></a>4.redis数据结构</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis 的作者antirez曾称其为一个数据结构服务器（<strong>data structures server</strong>），这是一个非常准确的表述，redis的所有功能就是将数据以其固有的几种结构保存，并提供给用户操作这几种结构的接口。我们可以想象我们在各种语言中的那些固有数据类型及其操作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis目前提供四种数据类型：<strong>string</strong>,<strong>list</strong>,<strong>set</strong>及<strong>zset</strong>(sorted set)和<strong>Hash</strong>。</p>
<ul>
<li><strong>set</strong>是集合，和我们数<strong>string</strong>是最简单的类型，你可以理解成与Memcached一模一个的类型，一个key对应一个value，其上支持的操作与Memcached的操作类似。但它的功能更丰富。</li>
<li><strong>list</strong>是一个链表结构，主要功能是push、pop、获取一个范围的所有值等等。操作中key理解为链表的名字。学中的集合概念相似，对集合的操作有添加删除元素，有对多个集合求交并差等操作。操作中key理解为集合的名字。</li>
<li><strong>zset</strong>是set的一个升级版本，他在set的基础上增加了一个顺序属性，这一属性在添加修改元素的时候可以指定，每次指定后，zset会自动重新按新的值调整顺序。可以理解了有两列的mysql表，一列存value，一列存顺序。操作中key理解为zset的名字。</li>
<li><strong>Hash</strong>数据类型允许用户用Redis存储对象类型,Hash数据类型的一个重要优点是,当你存储的数据对象只有很少几个key值时,数据存储的内存消耗会很小.更多关于Hash数据类型的说明请见: <a href="http://code.google.com/p/redis/wiki/Hashes" target="_blank" rel="external">http://code.google.com/p/redis/wiki/Hashes</a></li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在官网上给出了所有支持的接口列表，并副副附有详细的介绍，地址：<a href="http://code.google.com/p/redis/wiki/CommandReference" target="_blank" rel="external">http://code.google.com/p/redis/wiki/CommandReference</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另外，还提供了一个非常贴心的web命令行模拟页面，供初学者试用redis，地址：<a href="http://try.redis-db.com/" target="_blank" rel="external">http://try.redis-db.com/</a></p>
<h2 id="5-redis数据存储"><a href="#5-redis数据存储" class="headerlink" title="5.redis数据存储"></a>5.redis数据存储</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis的存储分为内存存储、磁盘存储和log文件三部分，配置文件中有三个参数对其进行配置。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>save seconds updates，save</strong>配置，指出在多长时间内，有多少次更新操作，就将数据同步到数据文件。这个可以多个条件配合，比如默认配置文件中的设置，就设置了三个条件。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>appendonly yes/no ，appendonly</strong>配置，指出是否在每次更新操作后进行日志记录，如果不开启，可能会在断电时导致一段时间内的数据丢失。因为redis本身同步数据文件是按上面的save条件来同步的，所以有的数据会在一段时间内只存在于内存中。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>appendfsync no/always/everysec ，appendfsync</strong>配置，<strong>no</strong>表示等操作系统进行数据缓存同步到磁盘，<strong>always</strong>表示每次更新操作后手动调用<strong>fsync()</strong>将数据写到磁盘，<strong>everysec</strong>表示每秒同步一次。</p>
<h2 id="6-redis主从配置"><a href="#6-redis主从配置" class="headerlink" title="6.redis主从配置"></a>6.redis主从配置</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis支持<strong>master-slave</strong>的主从配置，配置方法是在从机的配置文件中指定<strong>slaveof</strong>参数为主机的ip和port即可</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/7. Redis list 之增删改查/">Redis list 之增删改查</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<h2 id="一、增加"><a href="#一、增加" class="headerlink" title="一、增加"></a>一、增加</h2><ol>
<li>lpush [lpush key valus…]  类似于压栈操作，将元素放入头部</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; lpush plist ch0 ch1 ch2</div><div class="line">(<span class="built_in">integer</span>) 3</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 3</div><div class="line">1) <span class="string">"ch2"</span></div><div class="line">2) <span class="string">"ch1"</span></div><div class="line">3) <span class="string">"ch0"</span></div><div class="line">127.0.0.1:6379&gt; lpush plist ch4</div><div class="line">(<span class="built_in">integer</span>) 4</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 4</div><div class="line">1) <span class="string">"ch4"</span></div><div class="line">2) <span class="string">"ch2"</span></div><div class="line">3) <span class="string">"ch1"</span></div><div class="line">4) <span class="string">"ch0"</span></div></pre></td></tr></table></figure>
<ol>
<li>lpushx [lpushx key valus]:只能插入已经存在的key,且一次只能插入一次</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; lpushx pl ch</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">127.0.0.1:6379&gt; lpushx plist ch5 ch6</div><div class="line">(error) ERR wrong number of arguments <span class="keyword">for</span> <span class="string">'lpushx'</span> <span class="built_in">command</span></div><div class="line">127.0.0.1:6379&gt; lpushx plist ch5</div><div class="line">(<span class="built_in">integer</span>) 5</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 5</div><div class="line">1) <span class="string">"ch5"</span></div><div class="line">2) <span class="string">"ch4"</span></div><div class="line">3) <span class="string">"ch2"</span></div><div class="line">4) <span class="string">"ch1"</span></div><div class="line">5) <span class="string">"ch0"</span></div></pre></td></tr></table></figure>
<ol>
<li>rpush  [rpush key valus…] ：将元素push在list的尾部</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; rpush plist ch6</div><div class="line">(<span class="built_in">integer</span>) 6</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 6</div><div class="line">1) <span class="string">"ch5"</span></div><div class="line">2) <span class="string">"ch4"</span></div><div class="line">3) <span class="string">"ch2"</span></div><div class="line">4) <span class="string">"ch1"</span></div><div class="line">5) <span class="string">"ch0"</span></div><div class="line">6) <span class="string">"ch6"</span></div><div class="line">127.0.0.1:6379&gt; rpush plist ch7 ch8</div><div class="line">(<span class="built_in">integer</span>) 8</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 8</div><div class="line">1) <span class="string">"ch5"</span></div><div class="line">2) <span class="string">"ch4"</span></div><div class="line">3) <span class="string">"ch2"</span></div><div class="line">4) <span class="string">"ch1"</span></div><div class="line">5) <span class="string">"ch0"</span></div><div class="line">6) <span class="string">"ch6"</span></div><div class="line">7) <span class="string">"ch7"</span></div><div class="line">8) <span class="string">"ch8"</span></div></pre></td></tr></table></figure>
<ol>
<li><p>rpushx [rpushx key valus…] :相对于lpushx</p>
</li>
<li><p>linsert [linsert key before/after pivot value]:将值插入到pivot的前面或后面。返回列表元素个数。如果参照点pivot不存在不插入。如果有多个pivot，以离表头最近的为准</p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; linsert plist before ch1 chi</div><div class="line">(<span class="built_in">integer</span>) 9</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 9</div><div class="line">1) <span class="string">"ch5"</span></div><div class="line">2) <span class="string">"ch4"</span></div><div class="line">3) <span class="string">"ch2"</span></div><div class="line">4) <span class="string">"chi"</span></div><div class="line">5) <span class="string">"ch1"</span></div><div class="line">6) <span class="string">"ch0"</span></div><div class="line">7) <span class="string">"ch6"</span></div><div class="line">8) <span class="string">"ch7"</span></div><div class="line">9) <span class="string">"ch8"</span></div><div class="line">127.0.0.1:6379&gt; linsert plist before chii chi2</div><div class="line">(<span class="built_in">integer</span>) -1</div><div class="line">127.0.0.1:6379&gt; linsert plist after chi cha</div><div class="line">(<span class="built_in">integer</span>) 10</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line"> 1) <span class="string">"ch5"</span></div><div class="line"> 2) <span class="string">"ch4"</span></div><div class="line"> 3) <span class="string">"ch2"</span></div><div class="line"> 4) <span class="string">"chi"</span></div><div class="line"> 5) <span class="string">"cha"</span></div><div class="line"> 6) <span class="string">"ch1"</span></div><div class="line"> 7) <span class="string">"ch0"</span></div><div class="line"> 8) <span class="string">"ch6"</span></div><div class="line"> 9) <span class="string">"ch7"</span></div><div class="line">10) <span class="string">"ch8"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上插入操作均是返回list的长度</p>
<h2 id="二、删除"><a href="#二、删除" class="headerlink" title="二、删除"></a>二、删除</h2><ol>
<li>lpop 、rpop：分别为删除头部和尾部，返回被删除的元素</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; lpop plist</div><div class="line"><span class="string">"ch5"</span></div><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line">1) <span class="string">"ch4"</span></div><div class="line">2) <span class="string">"ch2"</span></div><div class="line">3) <span class="string">"chi"</span></div><div class="line">4) <span class="string">"cha"</span></div><div class="line">5) <span class="string">"ch1"</span></div><div class="line">6) <span class="string">"ch0"</span></div><div class="line">7) <span class="string">"ch6"</span></div><div class="line">8) <span class="string">"ch7"</span></div><div class="line">9) <span class="string">"ch8"</span></div><div class="line">127.0.0.1:6379&gt; rpop plist</div><div class="line"><span class="string">"ch8"</span></div><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line">1) <span class="string">"ch4"</span></div><div class="line">2) <span class="string">"ch2"</span></div><div class="line">3) <span class="string">"chi"</span></div><div class="line">4) <span class="string">"cha"</span></div><div class="line">5) <span class="string">"ch1"</span></div><div class="line">6) <span class="string">"ch0"</span></div><div class="line">7) <span class="string">"ch6"</span></div><div class="line">8) <span class="string">"ch7"</span></div></pre></td></tr></table></figure>
<ol>
<li>ltrim [ltrim key  range_l range_r]:保留区域类的元素，其他的删除</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; ltrim plist 0 3</div><div class="line">OK</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line">1) <span class="string">"ch4"</span></div><div class="line">2) <span class="string">"ch2"</span></div><div class="line">3) <span class="string">"chi"</span></div><div class="line">4) <span class="string">"cha"</span></div></pre></td></tr></table></figure>
<ol>
<li>lrem [lrem key count value] :移除等于value的元素，当count&gt;0时，从表头开始查找，移除count个；当count=0时，从表头开始查找，移除所有等于value的；当count&lt;0时，从表尾开始查找，移除|count| 个。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cout &gt;0</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line"> 1) <span class="string">"ch0"</span></div><div class="line"> 2) <span class="string">"ch0"</span></div><div class="line"> 3) <span class="string">"ch0"</span></div><div class="line"> 4) <span class="string">"ch4"</span></div><div class="line"> 5) <span class="string">"chi"</span></div><div class="line"> 6) <span class="string">"cha"</span></div><div class="line"> 7) <span class="string">"ch0"</span></div><div class="line"> 8) <span class="string">"ch0"</span></div><div class="line"> 9) <span class="string">"ch0"</span></div><div class="line">10) <span class="string">"ch0"</span></div><div class="line">127.0.0.1:6379&gt; lrem plist 5 ch0</div><div class="line">(<span class="built_in">integer</span>) 5</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line">1) <span class="string">"ch4"</span></div><div class="line">2) <span class="string">"chi"</span></div><div class="line">3) <span class="string">"cha"</span></div><div class="line">4) <span class="string">"ch0"</span></div><div class="line">5) <span class="string">"ch0"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;count &lt; 0</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line"> 1) <span class="string">"ch0"</span></div><div class="line"> 2) <span class="string">"ch9"</span></div><div class="line"> 3) <span class="string">"ch0"</span></div><div class="line"> 4) <span class="string">"ch0"</span></div><div class="line"> 5) <span class="string">"ch0"</span></div><div class="line"> 6) <span class="string">"ch4"</span></div><div class="line"> 7) <span class="string">"chi"</span></div><div class="line"> 8) <span class="string">"cha"</span></div><div class="line"> 9) <span class="string">"ch0"</span></div><div class="line">10) <span class="string">"ch0"</span></div><div class="line">127.0.0.1:6379&gt; lrem plist -5 ch0</div><div class="line">(<span class="built_in">integer</span>) 5</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line">1) <span class="string">"ch0"</span></div><div class="line">2) <span class="string">"ch9"</span></div><div class="line">3) <span class="string">"ch4"</span></div><div class="line">4) <span class="string">"chi"</span></div><div class="line">5) <span class="string">"cha"</span></div></pre></td></tr></table></figure>
<h2 id="三、修改"><a href="#三、修改" class="headerlink" title="三、修改"></a>三、修改</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;lset [lset key index value] : 设置列表指定索引的值，如果指定索引不存在则报错</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; lset plist 0 ch2</div><div class="line">OK</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line">1) <span class="string">"ch2"</span></div><div class="line">2) <span class="string">"ch9"</span></div><div class="line">3) <span class="string">"ch4"</span></div><div class="line">4) <span class="string">"chi"</span></div><div class="line">5) <span class="string">"cha"</span></div></pre></td></tr></table></figure>
<h2 id="四、查询"><a href="#四、查询" class="headerlink" title="四、查询"></a>四、查询</h2><ol>
<li>lindex [lindex key index]:通过索引index获取列表的元素、key&gt;=0从头到尾，key&lt;0从尾到头</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line">1) <span class="string">"ch2"</span></div><div class="line">2) <span class="string">"ch9"</span></div><div class="line">3) <span class="string">"ch4"</span></div><div class="line">4) <span class="string">"chi"</span></div><div class="line">5) <span class="string">"cha"</span></div><div class="line">127.0.0.1:6379&gt; lindex plist 0</div><div class="line"><span class="string">"ch2"</span></div><div class="line">127.0.0.1:6379&gt; lindex plist -0</div><div class="line"><span class="string">"ch2"</span></div><div class="line">127.0.0.1:6379&gt; lindex plist -1</div><div class="line"><span class="string">"cha"</span></div><div class="line">127.0.0.1:6379&gt; lindex plist 5</div><div class="line">(nil)</div></pre></td></tr></table></figure>
<ol>
<li>lrange [lrange key range_l range_r]: 0表头、-1表尾</li>
</ol>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/6. PHP-redis中文文档/">PHP-redis中文文档</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;phpredis是php的一个扩展，效率是相当高有链表排序功能，对创建内存级的模块业务关系很有用;以下是redis官方提供的命令使用技巧:</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载地址：<a href="https://github.com/owlient/phpredis（支持redis" target="_blank" rel="external">https://github.com/owlient/phpredis（支持redis</a> 2.0.4）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>Redis::__construct</strong>构造函数</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span> = new Redis();</div></pre></td></tr></table></figure>
<h2 id="connect-open-链接redis服务"><a href="#connect-open-链接redis服务" class="headerlink" title="connect, open 链接redis服务"></a>connect, open 链接redis服务</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;参数</p>
<ul>
<li>host: string，服务地址</li>
<li>port: int,端口号</li>
<li>timeout: float,链接时长 (可选, 默认为 0 ，不限链接时间)</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注: 在redis.conf中也有时间，默认为300</p>
<h2 id="pconnect-popen-不会主动关闭的链接"><a href="#pconnect-popen-不会主动关闭的链接" class="headerlink" title="pconnect, popen 不会主动关闭的链接"></a>pconnect, popen 不会主动关闭的链接</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;参考上面</p>
<h2 id="setOption"><a href="#setOption" class="headerlink" title="setOption"></a>setOption</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置redis模式</p>
<h2 id="getOption"><a href="#getOption" class="headerlink" title="getOption"></a>getOption</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看redis设置的模式</p>
<h2 id="ping"><a href="#ping" class="headerlink" title="ping"></a>ping</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看连接状态</p>
<h2 id="get"><a href="#get" class="headerlink" title="get"></a>get</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;得到某个key的值（string值）<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果该key不存在，return <strong>false</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>set</strong> 写入key 和 value（string值）<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果写入成功，return ture</p>
<h2 id="setex"><a href="#setex" class="headerlink" title="setex"></a>setex</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;带生存时间的写入值     </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;setex(<span class="string">'key'</span>, 3600, <span class="string">'value'</span>);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sets key → value, with 1h TTL.</p>
<h2 id="setnx"><a href="#setnx" class="headerlink" title="setnx"></a>setnx</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;判断是否重复的，写入值</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;setnx(<span class="string">'key'</span>, <span class="string">'value'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;setnx(<span class="string">'key'</span>, <span class="string">'value'</span>);</div></pre></td></tr></table></figure>
<h2 id="delete"><a href="#delete" class="headerlink" title="delete"></a>delete</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;删除指定key的值<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回已经删除key的个数（长整数）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;delete(<span class="string">'key1'</span>, <span class="string">'key2'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;delete(array(<span class="string">'key3'</span>, <span class="string">'key4'</span>, <span class="string">'key5'</span>));</div></pre></td></tr></table></figure>
<h2 id="ttl"><a href="#ttl" class="headerlink" title="ttl"></a>ttl</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;得到一个key的生存时间</p>
<h2 id="persist"><a href="#persist" class="headerlink" title="persist"></a>persist</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;移除生存时间到期的key<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果key到期 true 如果不到期 false</p>
<h2 id="mset（redis版本1-1以上才可以用）"><a href="#mset（redis版本1-1以上才可以用）" class="headerlink" title="mset（redis版本1.1以上才可以用）"></a>mset（redis版本1.1以上才可以用）</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;同时给多个key赋值</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;mset(array(<span class="string">'key0'</span> =&gt; <span class="string">'value0'</span>, <span class="string">'key1'</span> =&gt; <span class="string">'value1'</span>));</div></pre></td></tr></table></figure>
<h2 id="multi-exec-discard"><a href="#multi-exec-discard" class="headerlink" title="multi, exec, discard"></a>multi, exec, discard</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;进入或者退出事务模式<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;参数可选Redis::MULTI或Redis::PIPELINE. 默认是 Redis::MULTI<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis::MULTI：将多个操作当成一个事务执行<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis::PIPELINE:让（多条）执行命令简单的，更加快速的发送给服务器，但是没   有任何原子性的保证<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;discard:删除一个事务<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回值<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;multi()，返回一个redis对象，并进入multi-mode模式，一旦进入multi-mode模式，以后调用的所有方法都会返回相同的对象，只到exec(）方法被调用。    </p>
<h2 id="watch-unwatch（代码测试后，不能达到所说的效果）"><a href="#watch-unwatch（代码测试后，不能达到所说的效果）" class="headerlink" title="watch, unwatch（代码测试后，不能达到所说的效果）"></a>watch, unwatch（代码测试后，不能达到所说的效果）</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;监测一个key的值是否被其它的程序更改。如果这个key在watch 和 exec （方法）间被修改，这个 MULTI/EXEC 事务的执行将失败（return false）<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;unwatch      取消被这个程序监测的所有key<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 参数，一对key的列表    </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;watch(<span class="string">'x'</span>);</div><div class="line"></div><div class="line"><span class="variable">$ret</span> = <span class="variable">$redis</span>-&gt;multi() -&gt;incr(<span class="string">'x'</span>) -&gt;<span class="built_in">exec</span>();</div></pre></td></tr></table></figure>
<h2 id="subscribe"><a href="#subscribe" class="headerlink" title="subscribe *"></a>subscribe *</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;方法回调。注意，该方法可能在未来里发生改变</p>
<h2 id="publish"><a href="#publish" class="headerlink" title="publish *"></a>publish *</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;发表内容到某一个通道。注意，该方法可能在未来里发生改变</p>
<h2 id="exists"><a href="#exists" class="headerlink" title="exists"></a>exists</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;判断key是否存在。存在 true 不在 false</p>
<h2 id="incr-incrBy"><a href="#incr-incrBy" class="headerlink" title="incr, incrBy"></a>incr, incrBy</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;key中的值进行自增1，如果填写了第二个参数，者自增第二个参数所填的值</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;incr(<span class="string">'key1'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;incrBy(<span class="string">'key1'</span>, 10);</div></pre></td></tr></table></figure>
<h2 id="decr-decrBy"><a href="#decr-decrBy" class="headerlink" title="decr, decrBy"></a>decr, decrBy</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;做减法，使用方法同incr</p>
<h2 id="getMultiple"><a href="#getMultiple" class="headerlink" title="getMultiple"></a>getMultiple</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;传参<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;由key组成的数组<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回参数<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果key存在返回value，不存在返回false</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;<span class="built_in">set</span>(<span class="string">'key1'</span>, <span class="string">'value1'</span>); <span class="variable">$redis</span>-&gt;<span class="built_in">set</span>(<span class="string">'key2'</span>, <span class="string">'value2'</span>); <span class="variable">$redis</span>-&gt;<span class="built_in">set</span>(<span class="string">'key3'</span>, <span class="string">'value3'</span>); <span class="variable">$redis</span>-&gt;getMultiple(array(<span class="string">'key1'</span>, <span class="string">'key2'</span>, <span class="string">'key3'</span>));</div><div class="line"><span class="variable">$redis</span>-&gt;lRem(<span class="string">'key1'</span>, <span class="string">'A'</span>, 2);</div><div class="line"><span class="variable">$redis</span>-&gt;lRange(<span class="string">'key1'</span>, 0, -1);</div></pre></td></tr></table></figure>
<h2 id="list相关操作"><a href="#list相关操作" class="headerlink" title="list相关操作"></a>list相关操作</h2><h3 id="lPush"><a href="#lPush" class="headerlink" title="lPush"></a>lPush</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;lPush(key, value);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在名称为key的list左边（头）添加一个值为value的 元素</p>
<h3 id="rPush"><a href="#rPush" class="headerlink" title="rPush"></a>rPush</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;rPush(key, value);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在名称为key的list右边（尾）添加一个值为value的 元素</p>
<h3 id="lPushx-rPushx"><a href="#lPushx-rPushx" class="headerlink" title="lPushx/rPushx"></a>lPushx/rPushx</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;lPushx(key, value);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在名称为key的list左边(头)/右边（尾）添加一个值为value的元素,如果value已经存在，则不添加</p>
<h3 id="lPop-rPop"><a href="#lPop-rPop" class="headerlink" title="lPop/rPop"></a>lPop/rPop</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;lPop(<span class="string">'key'</span>);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出名称为key的list左(头)起/右（尾）起的第一个元素，删除该元素</p>
<h3 id="blPop-brPop"><a href="#blPop-brPop" class="headerlink" title="blPop/brPop"></a>blPop/brPop</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;blPop(<span class="string">'key1'</span>, <span class="string">'key2'</span>, 10);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;lpop命令的block版本。即当timeout为0时，若遇到名称为key i的list不存在或该list为空，则命令结束。如果timeout&gt;0，则遇到上述情况时，等待timeout秒，如果问题没有解决，则对keyi+1开始的list执行pop操作</p>
<h3 id="lSize"><a href="#lSize" class="headerlink" title="lSize"></a>lSize</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;lSize(<span class="string">'key'</span>);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为key的list有多少个元素</p>
<h3 id="lIndex-lGet"><a href="#lIndex-lGet" class="headerlink" title="lIndex, lGet"></a>lIndex, lGet</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;lGet(<span class="string">'key'</span>, 0);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为key的list中index位置的元素</p>
<h3 id="lSet"><a href="#lSet" class="headerlink" title="lSet"></a>lSet</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;lSet(<span class="string">'key'</span>, 0, <span class="string">'X'</span>);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;给名称为key的list中index位置的元素赋值为value</p>
<h3 id="lRange-lGetRange"><a href="#lRange-lGetRange" class="headerlink" title="lRange, lGetRange"></a>lRange, lGetRange</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;lRange(<span class="string">'key1'</span>, 0, -1);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为key的list中start至end之间的元素（end为 -1 ，返回所有）</p>
<h3 id="lTrim-listTrim"><a href="#lTrim-listTrim" class="headerlink" title="lTrim, listTrim"></a>lTrim, listTrim</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;lTrim(<span class="string">'key'</span>, start, end);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;截取名称为key的list，保留start至end之间的元素</p>
<h3 id="lRem-lRemove"><a href="#lRem-lRemove" class="headerlink" title="lRem, lRemove"></a>lRem, lRemove</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;lRem(<span class="string">'key'</span>, <span class="string">'A'</span>, 2);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;删除count个名称为key的list中值为value的元素。count为0，删除所有值为value的元素，count&gt;0从头至尾删除count个值为value的元素，count &lt; 0从尾到头删除|count|个值为value的元素</p>
<h3 id="lInsert"><a href="#lInsert" class="headerlink" title="lInsert"></a>lInsert</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在名称为为key的list中，找到值为pivot 的value，并根据参数Redis::BEFORE |  Redis::AFTER，来确定，newvalue 是放在 pivot 的前面，或者后面。如果key不存在，  不会插入，如果 pivot不存在，return -1</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;delete(<span class="string">'key1'</span>); <span class="variable">$redis</span>-&gt;lInsert(<span class="string">'key1'</span>, Redis::AFTER, <span class="string">'A'</span>, <span class="string">'X'</span>); <span class="variable">$redis</span>-&gt;lPush(<span class="string">'key1'</span>, <span class="string">'A'</span>); <span class="variable">$redis</span>-&gt;lPush(<span class="string">'key1'</span>, <span class="string">'B'</span>); <span class="variable">$redis</span>-&gt;lPush(<span class="string">'key1'</span>, <span class="string">'C'</span>); <span class="variable">$redis</span>-&gt;lInsert(<span class="string">'key1'</span>, Redis::BEFORE, <span class="string">'C'</span>, <span class="string">'X'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;lRange(<span class="string">'key1'</span>, 0, -1);</div><div class="line"><span class="variable">$redis</span>-&gt;lInsert(<span class="string">'key1'</span>, Redis::AFTER, <span class="string">'C'</span>, <span class="string">'Y'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;lRange(<span class="string">'key1'</span>, 0, -1);</div><div class="line"><span class="variable">$redis</span>-&gt;lInsert(<span class="string">'key1'</span>, Redis::AFTER, <span class="string">'W'</span>, <span class="string">'value'</span>);</div></pre></td></tr></table></figure>
<h3 id="rpoplpush"><a href="#rpoplpush" class="headerlink" title="rpoplpush"></a>rpoplpush</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回并删除名称为srckey的list的尾元素，并将该元素添加到名称为dstkey的list的头部</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;delete(<span class="string">'x'</span>, <span class="string">'y'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;lPush(<span class="string">'x'</span>, <span class="string">'abc'</span>); <span class="variable">$redis</span>-&gt;lPush(<span class="string">'x'</span>, <span class="string">'def'</span>); <span class="variable">$redis</span>-&gt;lPush(<span class="string">'y'</span>, <span class="string">'123'</span>); <span class="variable">$redis</span>-&gt;lPush(<span class="string">'y'</span>, <span class="string">'456'</span>); // move the last of x to the front of y. var_dump(<span class="variable">$redis</span>-&gt;rpoplpush(<span class="string">'x'</span>, <span class="string">'y'</span>));</div><div class="line">var_dump(<span class="variable">$redis</span>-&gt;lRange(<span class="string">'x'</span>, 0, -1));</div><div class="line">var_dump(<span class="variable">$redis</span>-&gt;lRange(<span class="string">'y'</span>, 0, -1)); </div><div class="line"></div><div class="line">string(3) <span class="string">"abc"</span> </div><div class="line">array(1) &#123; [0]=&gt; string(3) <span class="string">"def"</span> &#125; </div><div class="line">array(3) &#123; [0]=&gt; string(3) <span class="string">"abc"</span> [1]=&gt; string(3) <span class="string">"456"</span> [2]=&gt; string(3) <span class="string">"123"</span> &#125;</div></pre></td></tr></table></figure>
<h2 id="SET操作相关"><a href="#SET操作相关" class="headerlink" title="SET操作相关"></a>SET操作相关</h2><h3 id="sAdd"><a href="#sAdd" class="headerlink" title="sAdd"></a>sAdd</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;向名称为key的set中添加元素value,如果value存在，不写入，return false</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;sAdd(key , value);</div></pre></td></tr></table></figure>
<h3 id="sRem-sRemove"><a href="#sRem-sRemove" class="headerlink" title="sRem, sRemove"></a>sRem, sRemove</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;删除名称为key的set中的元素value</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;sAdd(<span class="string">'key1'</span> , <span class="string">'set1'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;sAdd(<span class="string">'key1'</span> , <span class="string">'set2'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;sAdd(<span class="string">'key1'</span> , <span class="string">'set3'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;sRem(<span class="string">'key1'</span>, <span class="string">'set2'</span>);</div></pre></td></tr></table></figure>
<h3 id="sMove"><a href="#sMove" class="headerlink" title="sMove"></a>sMove</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将value元素从名称为srckey的集合移到名称为dstkey的集合</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;sMove(seckey, dstkey, value);</div></pre></td></tr></table></figure>
<h3 id="sIsMember-sContains"><a href="#sIsMember-sContains" class="headerlink" title="sIsMember, sContains"></a>sIsMember, sContains</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;名称为key的集合中查找是否有value元素，有ture 没有 false</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;sIsMember(key, value);</div></pre></td></tr></table></figure>
<h3 id="sCard-sSize"><a href="#sCard-sSize" class="headerlink" title="sCard, sSize"></a>sCard, sSize</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为key的set的元素个数</p>
<h3 id="sPop"><a href="#sPop" class="headerlink" title="sPop"></a>sPop</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;随机返回并删除名称为key的set中一个元素</p>
<h3 id="sRandMember"><a href="#sRandMember" class="headerlink" title="sRandMember"></a>sRandMember</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;随机返回名称为key的set中一个元素，不删除</p>
<h3 id="sInter"><a href="#sInter" class="headerlink" title="sInter"></a>sInter</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;求交集</p>
<h3 id="sInterStore"><a href="#sInterStore" class="headerlink" title="sInterStore"></a>sInterStore</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;求交集并将交集保存到output的集合</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;sInterStore(<span class="string">'output'</span>, <span class="string">'key1'</span>, <span class="string">'key2'</span>, <span class="string">'key3'</span>)</div></pre></td></tr></table></figure>
<h3 id="sUnion"><a href="#sUnion" class="headerlink" title="sUnion"></a>sUnion</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;求并集</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;sUnion(<span class="string">'s0'</span>, <span class="string">'s1'</span>, <span class="string">'s2'</span>);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;s0,s1,s2 同时求并集</p>
<h3 id="sUnionStore"><a href="#sUnionStore" class="headerlink" title="sUnionStore"></a>sUnionStore</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;求并集并将并集保存到output的集合</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;sUnionStore(<span class="string">'output'</span>, <span class="string">'key1'</span>, <span class="string">'key2'</span>, <span class="string">'key3'</span>)；</div></pre></td></tr></table></figure>
<h3 id="sDiff"><a href="#sDiff" class="headerlink" title="sDiff"></a>sDiff</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;求差集</p>
<h3 id="sDiffStore"><a href="#sDiffStore" class="headerlink" title="sDiffStore"></a>sDiffStore</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;求差集并将差集保存到output的集合</p>
<h3 id="sMembers-sGetMembers"><a href="#sMembers-sGetMembers" class="headerlink" title="sMembers, sGetMembers"></a>sMembers, sGetMembers</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为key的set的所有元素</p>
<h3 id="sort"><a href="#sort" class="headerlink" title="sort"></a>sort</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;排序，分页等</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;参数</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="string">'by'</span> =&gt; <span class="string">'some_pattern_*'</span>,</div><div class="line"><span class="string">'limit'</span> =&gt; array(0, 1),</div><div class="line"><span class="string">'get'</span> =&gt; <span class="string">'some_other_pattern_*'</span> or an array of patterns,</div><div class="line"><span class="string">'sort'</span> =&gt; <span class="string">'asc'</span> or <span class="string">'desc'</span>,</div><div class="line"><span class="string">'alpha'</span> =&gt; TRUE,</div><div class="line"><span class="string">'store'</span> =&gt; <span class="string">'external-key'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例子</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;delete(<span class="string">'s'</span>); <span class="variable">$redis</span>-&gt;sadd(<span class="string">'s'</span>, 5); <span class="variable">$redis</span>-&gt;sadd(<span class="string">'s'</span>, 4); <span class="variable">$redis</span>-&gt;sadd(<span class="string">'s'</span>, 2); <span class="variable">$redis</span>-&gt;sadd(<span class="string">'s'</span>, 1); <span class="variable">$redis</span>-&gt;sadd(<span class="string">'s'</span>, 3);</div><div class="line">var_dump(<span class="variable">$redis</span>-&gt;sort(<span class="string">'s'</span>)); // 1,2,3,4,5</div><div class="line">var_dump(<span class="variable">$redis</span>-&gt;sort(<span class="string">'s'</span>, array(<span class="string">'sort'</span> =&gt; <span class="string">'desc'</span>))); // 5,4,3,2,1</div><div class="line">var_dump(<span class="variable">$redis</span>-&gt;sort(<span class="string">'s'</span>, array(<span class="string">'sort'</span> =&gt; <span class="string">'desc'</span>, <span class="string">'store'</span> =&gt; <span class="string">'out'</span>))); // (int)5</div></pre></td></tr></table></figure>
<h2 id="string命令"><a href="#string命令" class="headerlink" title="string命令"></a>string命令</h2><h3 id="getSet"><a href="#getSet" class="headerlink" title="getSet"></a>getSet</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回原来key中的值，并将value写入key</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;<span class="built_in">set</span>(<span class="string">'x'</span>, <span class="string">'42'</span>);</div><div class="line"><span class="variable">$exValue</span> = <span class="variable">$redis</span>-&gt;getSet(<span class="string">'x'</span>, <span class="string">'lol'</span>); // <span class="built_in">return</span> <span class="string">'42'</span>, replaces x by <span class="string">'lol'</span></div><div class="line"><span class="variable">$newValue</span> = <span class="variable">$redis</span>-&gt;get(<span class="string">'x'</span>)<span class="string">' // return '</span>lol<span class="string">'</span></div></pre></td></tr></table></figure>
<h3 id="append"><a href="#append" class="headerlink" title="append"></a>append</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;string，名称为key的string的值在后面加上value</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;<span class="built_in">set</span>(<span class="string">'key'</span>, <span class="string">'value1'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;append(<span class="string">'key'</span>, <span class="string">'value2'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;get(<span class="string">'key'</span>);</div></pre></td></tr></table></figure>
<h3 id="getRange（方法不存在）"><a href="#getRange（方法不存在）" class="headerlink" title="getRange（方法不存在）"></a>getRange（方法不存在）</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为key的string中start至end之间的字符</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;<span class="built_in">set</span>(<span class="string">'key'</span>, <span class="string">'string value'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;getRange(<span class="string">'key'</span>, 0, 5);</div><div class="line"><span class="variable">$redis</span>-&gt;getRange(<span class="string">'key'</span>, -5, -1);</div></pre></td></tr></table></figure>
<h3 id="setRange（方法不存在）"><a href="#setRange（方法不存在）" class="headerlink" title="setRange（方法不存在）"></a>setRange（方法不存在）</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;改变key的string中start至end之间的字符为value</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;<span class="built_in">set</span>(<span class="string">'key'</span>, <span class="string">'Hello world'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;setRange(<span class="string">'key'</span>, 6, <span class="string">"redis"</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;get(<span class="string">'key'</span>);</div></pre></td></tr></table></figure>
<h3 id="strlen"><a href="#strlen" class="headerlink" title="strlen"></a>strlen</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;得到key的string的长度</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;strlen(<span class="string">'key'</span>);</div></pre></td></tr></table></figure>
<h3 id="getBit-setBit"><a href="#getBit-setBit" class="headerlink" title="getBit/setBit"></a>getBit/setBit</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回2进制信息</p>
<h2 id="zset（sorted-set）操作相关"><a href="#zset（sorted-set）操作相关" class="headerlink" title="zset（sorted set）操作相关"></a>zset（sorted set）操作相关</h2><h3 id="zAdd-key-score-member"><a href="#zAdd-key-score-member" class="headerlink" title="zAdd(key, score, member)"></a>zAdd(key, score, member)</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;向名称为key的zset中添加元素member，score用于排序。如果该元素已经存在，则根据score更新该元素的顺序。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;zAdd(<span class="string">'key'</span>, 1, <span class="string">'val1'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;zAdd(<span class="string">'key'</span>, 0, <span class="string">'val0'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;zAdd(<span class="string">'key'</span>, 5, <span class="string">'val5'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;zRange(<span class="string">'key'</span>, 0, -1); // array(val0, val1, val5)</div></pre></td></tr></table></figure>
<h3 id="zRange-key-start-end-withscores"><a href="#zRange-key-start-end-withscores" class="headerlink" title="zRange(key, start, end,withscores)"></a>zRange(key, start, end,withscores)</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为key的zset（元素已按score从小到大排序）中的index从start到end的所有元素</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;zAdd(<span class="string">'key1'</span>, 0, <span class="string">'val0'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;zAdd(<span class="string">'key1'</span>, 2, <span class="string">'val2'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;zAdd(<span class="string">'key1'</span>, 10, <span class="string">'val10'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;zRange(<span class="string">'key1'</span>, 0, -1); // with scores <span class="variable">$redis</span>-&gt;zRange(<span class="string">'key1'</span>, 0, -1, <span class="literal">true</span>);</div></pre></td></tr></table></figure>
<h2 id="zDelete-zRem"><a href="#zDelete-zRem" class="headerlink" title="zDelete, zRem"></a>zDelete, zRem</h2><h3 id="zRem-key-member"><a href="#zRem-key-member" class="headerlink" title="zRem(key, member)"></a>zRem(key, member)</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;删除名称为key的zset中的元素member</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;zAdd(<span class="string">'key'</span>, 0, <span class="string">'val0'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;zAdd(<span class="string">'key'</span>, 2, <span class="string">'val2'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;zAdd(<span class="string">'key'</span>, 10, <span class="string">'val10'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;zDelete(<span class="string">'key'</span>, <span class="string">'val2'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;zRange(<span class="string">'key'</span>, 0, -1);</div></pre></td></tr></table></figure>
<h3 id="zRevRange-key-start-end-withscores"><a href="#zRevRange-key-start-end-withscores" class="headerlink" title="zRevRange(key, start, end,withscores)"></a>zRevRange(key, start, end,withscores)</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为key的zset（元素已按score从大到小排序）中的index从start到end的所有元素.withscores: 是否输出socre的值，默认false，不输出</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;zAdd(<span class="string">'key'</span>, 0, <span class="string">'val0'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;zAdd(<span class="string">'key'</span>, 2, <span class="string">'val2'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;zAdd(<span class="string">'key'</span>, 10, <span class="string">'val10'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;zRevRange(<span class="string">'key'</span>, 0, -1); // with scores <span class="variable">$redis</span>-&gt;zRevRange(<span class="string">'key'</span>, 0, -1, <span class="literal">true</span>);</div></pre></td></tr></table></figure>
<h3 id="zRangeByScore-zRevRangeByScore"><a href="#zRangeByScore-zRevRangeByScore" class="headerlink" title="zRangeByScore, zRevRangeByScore"></a>zRangeByScore, zRevRangeByScore</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;zRangeByScore(key, star, end, array(withscores， <span class="built_in">limit</span> ));</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为key的zset中score &gt;= star且score &lt;= end的所有元素</p>
<h3 id="zCount"><a href="#zCount" class="headerlink" title="zCount"></a>zCount</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;zCount(key, star, end);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为key的zset中score &gt;= star且score &lt;= end的所有元素的个数</p>
<h3 id="zRemRangeByScore-zDeleteRangeByScore"><a href="#zRemRangeByScore-zDeleteRangeByScore" class="headerlink" title="zRemRangeByScore, zDeleteRangeByScore"></a>zRemRangeByScore, zDeleteRangeByScore</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;zRemRangeByScore(<span class="string">'key'</span>, star, end);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;删除名称为key的zset中score &gt;= star且score &lt;= end的所有元素，返回删除个数</p>
<h3 id="zSize-zCard"><a href="#zSize-zCard" class="headerlink" title="zSize, zCard"></a>zSize, zCard</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为key的zset的所有元素的个数</p>
<h3 id="zScore"><a href="#zScore" class="headerlink" title="zScore"></a>zScore</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;zScore(key, val2);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为key的zset中元素val2的score</p>
<h3 id="zRank-zRevRank"><a href="#zRank-zRevRank" class="headerlink" title="zRank, zRevRank"></a>zRank, zRevRank</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;zRevRank(key, val);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为key的zset（元素已按score从小到大排序）中val元素的rank（即index，从0开始），若没有val元素，返回“null”。zRevRank 是从大到小排序</p>
<h3 id="zIncrBy"><a href="#zIncrBy" class="headerlink" title="zIncrBy"></a>zIncrBy</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;zIncrBy(<span class="string">'key'</span>, increment, <span class="string">'member'</span>);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果在名称为key的zset中已经存在元素member，则该元素的score增加increment；否则向集合中添加该元素，其score的值为increment</p>
<h3 id="zUnion-zInter"><a href="#zUnion-zInter" class="headerlink" title="zUnion/zInter"></a>zUnion/zInter</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;参数</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">keyOutput</div><div class="line">arrayZSetKeys</div><div class="line">arrayWeights</div><div class="line">aggregateFunction Either <span class="string">"SUM"</span>, <span class="string">"MIN"</span>, or <span class="string">"MAX"</span>: defines the behaviour to use on duplicate entries during the zUnion.</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对N个zset求并集和交集，并将最后的集合保存在dstkeyN中。对于集合中每一个元素的score，在进行AGGREGATE运算前，都要乘以对于的WEIGHT参数。如果没有提供WEIGHT，默认为1。默认的AGGREGATE是SUM，即结果集合中元素的score是所有集合对应元素进行SUM运算的值，而MIN和MAX是指，结果集合中元素的score是所有集合对应元素  中最小值和最大值。</p>
<h2 id="Hash操作"><a href="#Hash操作" class="headerlink" title="Hash操作"></a>Hash操作</h2><h3 id="hSet"><a href="#hSet" class="headerlink" title="hSet"></a>hSet</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;hSet(<span class="string">'h'</span>, <span class="string">'key1'</span>, <span class="string">'hello'</span>);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;向名称为h的hash中添加元素key1—&gt;hello</p>
<h3 id="hGet"><a href="#hGet" class="headerlink" title="hGet"></a>hGet</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;hGet(<span class="string">'h'</span>, <span class="string">'key1'</span>);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为h的hash中key1对应的value（hello）</p>
<h3 id="hLen"><a href="#hLen" class="headerlink" title="hLen"></a>hLen</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;hLen(<span class="string">'h'</span>);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为h的hash中元素个数</p>
<h3 id="hDel"><a href="#hDel" class="headerlink" title="hDel"></a>hDel</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;hDel(<span class="string">'h'</span>, <span class="string">'key1'</span>);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;删除名称为h的hash中键为key1的域</p>
<h3 id="hKeys"><a href="#hKeys" class="headerlink" title="hKeys"></a>hKeys</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;hKeys(<span class="string">'h'</span>);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为key的hash中所有键</p>
<h3 id="hVals"><a href="#hVals" class="headerlink" title="hVals"></a>hVals</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;hVals(<span class="string">'h'</span>)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为h的hash中所有键对应的value</p>
<h3 id="hGetAll"><a href="#hGetAll" class="headerlink" title="hGetAll"></a>hGetAll</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;hGetAll(<span class="string">'h'</span>);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为h的hash中所有的键（field）及其对应的value</p>
<h3 id="hExists"><a href="#hExists" class="headerlink" title="hExists"></a>hExists</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;hExists(<span class="string">'h'</span>, <span class="string">'a'</span>);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;名称为h的hash中是否存在键名字为a的域</p>
<h3 id="hIncrBy"><a href="#hIncrBy" class="headerlink" title="hIncrBy"></a>hIncrBy</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;hIncrBy(<span class="string">'h'</span>, <span class="string">'x'</span>, 2);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将名称为h的hash中x的value增加2</p>
<h3 id="hMset"><a href="#hMset" class="headerlink" title="hMset"></a>hMset</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;hMset(<span class="string">'user:1'</span>, array(<span class="string">'name'</span> =&gt; <span class="string">'Joe'</span>, <span class="string">'salary'</span> =&gt; 2000));</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;向名称为key的hash中批量添加元素</p>
<h3 id="hMGet"><a href="#hMGet" class="headerlink" title="hMGet"></a>hMGet</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;hmGet(<span class="string">'h'</span>, array(<span class="string">'field1'</span>, <span class="string">'field2'</span>));</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回名称为h的hash中field1,field2对应的value</p>
<h2 id="redis-操作相关"><a href="#redis-操作相关" class="headerlink" title="redis 操作相关"></a>redis 操作相关</h2><h3 id="flushDB"><a href="#flushDB" class="headerlink" title="flushDB"></a>flushDB</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;清空当前数据库</p>
<h3 id="flushAll"><a href="#flushAll" class="headerlink" title="flushAll"></a>flushAll</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;清空所有数据库</p>
<h3 id="randomKey"><a href="#randomKey" class="headerlink" title="randomKey"></a>randomKey</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;随机返回key空间的一个key</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$key</span> = <span class="variable">$redis</span>-&gt;randomKey();</div></pre></td></tr></table></figure>
<h3 id="select"><a href="#select" class="headerlink" title="select"></a>select</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;选择一个数据库</p>
<h3 id="move"><a href="#move" class="headerlink" title="move"></a>move</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;转移一个key到另外一个数据库</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;select(0); // switch to DB 0</div><div class="line"><span class="variable">$redis</span>-&gt;<span class="built_in">set</span>(<span class="string">'x'</span>, <span class="string">'42'</span>); // write 42 to x</div><div class="line"><span class="variable">$redis</span>-&gt;move(<span class="string">'x'</span>, 1); // move to DB 1</div><div class="line"><span class="variable">$redis</span>-&gt;select(1); // switch to DB 1</div><div class="line"><span class="variable">$redis</span>-&gt;get(<span class="string">'x'</span>); // will <span class="built_in">return</span> 42</div></pre></td></tr></table></figure>
<h3 id="rename-renameKey"><a href="#rename-renameKey" class="headerlink" title="rename, renameKey"></a>rename, renameKey</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;给key重命名</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;<span class="built_in">set</span>(<span class="string">'x'</span>, <span class="string">'42'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;rename(<span class="string">'x'</span>, <span class="string">'y'</span>);</div><div class="line"><span class="variable">$redis</span>-&gt;get(<span class="string">'y'</span>); // → 42</div><div class="line"><span class="variable">$redis</span>-&gt;get(<span class="string">'x'</span>); // → `FALSE`</div></pre></td></tr></table></figure>
<h3 id="renameNx"><a href="#renameNx" class="headerlink" title="renameNx"></a>renameNx</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;与remane类似，但是，如果重新命名的名字已经存在，不会替换成功</p>
<h3 id="setTimeout-expire"><a href="#setTimeout-expire" class="headerlink" title="setTimeout, expire"></a>setTimeout, expire</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设定一个key的活动时间（s）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;setTimeout(<span class="string">'x'</span>, 3);</div></pre></td></tr></table></figure>
<h3 id="expireAt"><a href="#expireAt" class="headerlink" title="expireAt"></a>expireAt</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;key存活到一个unix时间戳时间</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;expireAt(<span class="string">'x'</span>, time() + 3);</div></pre></td></tr></table></figure>
<h3 id="keys-getKeys"><a href="#keys-getKeys" class="headerlink" title="keys, getKeys"></a>keys, getKeys</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回满足给定pattern的所有key</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$keyWithUserPrefix</span> = <span class="variable">$redis</span>-&gt;keys(<span class="string">'user*'</span>);</div></pre></td></tr></table></figure>
<h3 id="dbSize"><a href="#dbSize" class="headerlink" title="dbSize"></a>dbSize</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看现在数据库有多少key</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$count</span> = <span class="variable">$redis</span>-&gt;dbSize();</div></pre></td></tr></table></figure>
<h3 id="auth"><a href="#auth" class="headerlink" title="auth"></a>auth</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;密码认证</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;auth(<span class="string">'foobared'</span>);</div></pre></td></tr></table></figure>
<h3 id="bgrewriteaof"><a href="#bgrewriteaof" class="headerlink" title="bgrewriteaof"></a>bgrewriteaof</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用aof来进行数据库持久化</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;bgrewriteaof();</div></pre></td></tr></table></figure>
<h3 id="slaveof"><a href="#slaveof" class="headerlink" title="slaveof"></a>slaveof</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;选择从服务器</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$redis</span>-&gt;slaveof(<span class="string">'10.0.1.7'</span>, 6379);</div></pre></td></tr></table></figure>
<h3 id="save"><a href="#save" class="headerlink" title="save"></a>save</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将数据同步保存到磁盘</p>
<h3 id="bgsave"><a href="#bgsave" class="headerlink" title="bgsave"></a>bgsave</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将数据异步保存到磁盘</p>
<h3 id="lastSave"><a href="#lastSave" class="headerlink" title="lastSave"></a>lastSave</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回上次成功将数据保存到磁盘的Unix时戳</p>
<h3 id="info"><a href="#info" class="headerlink" title="info"></a>info</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回redis的版本信息等详情</p>
<h3 id="type"><a href="#type" class="headerlink" title="type"></a>type</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回key的类型值</p>
<ul>
<li>string: Redis::REDIS_STRING</li>
<li>set: Redis::REDIS_SET</li>
<li>list: Redis::REDIS_LIST</li>
<li>zset: Redis::REDIS_ZSET</li>
<li>hash: Redis::REDIS_HASH</li>
<li>other: Redis::REDIS_NOT_FOUND</li>
</ul>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/4. Redis 常用命令/">Redis 常用命令</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<h2 id="1-string-常用操作"><a href="#1-string-常用操作" class="headerlink" title="1.string 常用操作"></a>1.string 常用操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">set</span> key1 yanyi</div><div class="line">get key1  </div><div class="line"><span class="built_in">set</span> key1 yanyi1           <span class="comment"># 一个key对应一个value，多次赋值，会覆盖前面的value</span></div><div class="line">setnx key2 aaa            <span class="comment"># 返回1</span></div><div class="line">get key2</div><div class="line">setnx key2 bbb            <span class="comment"># 返回0;说明：setnx 如果key存在，则返回0，不存在会直接创建这个key</span></div><div class="line">setex key3 10 1           <span class="comment"># 这个是用来给key设定过期时间的</span></div><div class="line">mset key1 1 key2 2 key3 3 <span class="comment"># 同时设置多个key</span></div><div class="line">mget key1 key2 key3</div></pre></td></tr></table></figure>
<h2 id="2-hash-数据常用操作"><a href="#2-hash-数据常用操作" class="headerlink" title="2.hash 数据常用操作"></a>2.hash 数据常用操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">hset user1  name aming               <span class="comment"># 建立hash</span></div><div class="line">hset user1 age 30</div><div class="line">hset user1 job  it</div><div class="line">hgetall user1</div><div class="line">hmset user2 name yanyi age 30 job it <span class="comment"># 批量建立键值对</span></div><div class="line">hmget user2</div><div class="line">hmget user2 name age  job</div><div class="line">hdel user2 job                       <span class="comment"># 删除指定filed</span></div><div class="line">hkeys user2                          <span class="comment"># 打印所有的key</span></div><div class="line">hvals user2                          <span class="comment"># 打印所有的values</span></div><div class="line">hlen user2                           <span class="comment"># 查看hash有几个filed</span></div></pre></td></tr></table></figure>
<h2 id="3-list-数据常用操作"><a href="#3-list-数据常用操作" class="headerlink" title="3.list 数据常用操作"></a>3.list 数据常用操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">lpush lista  a                 <span class="comment"># 从左侧压入一个元素</span></div><div class="line">lpush lista b</div><div class="line">lrange lista  0 -1             <span class="comment"># 0 为头，-1为尾</span></div><div class="line">lpop lista                     <span class="comment"># 从左侧取出</span></div><div class="line">rpush lista  1                 <span class="comment"># 从右侧压入一个元素</span></div><div class="line">rpush lista  2</div><div class="line">lrange lista  0 -1</div><div class="line">rpop lista                     <span class="comment"># 从右侧取第一个元素</span></div><div class="line">linsert  lista  before  2 3    <span class="comment"># 在2的前面插入一个元素为3</span></div><div class="line">lset lista 4 bbb               <span class="comment"># 把第5个元素修改为bbb</span></div><div class="line">lindex lista 0                 <span class="comment"># 查看第1个元素</span></div><div class="line">lindex lista 3                 <span class="comment"># 查看第4个元素</span></div><div class="line">llen lista                     <span class="comment"># 查看链表中有几个元素</span></div></pre></td></tr></table></figure>
<h2 id="4-set-数据常用操作"><a href="#4-set-数据常用操作" class="headerlink" title="4.set 数据常用操作"></a>4.set 数据常用操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">sadd seta aaa               <span class="comment">#向集合seta中放入元素</span></div><div class="line">smembers seta               <span class="comment"># 查看集合中的所有元素</span></div><div class="line">srem  seta    aaa           <span class="comment"># 删除元素</span></div><div class="line">spop  seta                  <span class="comment"># 随机取出一个元素，删除</span></div><div class="line">sdiff  seta  setb           <span class="comment"># 求差集，以seta为标准</span></div><div class="line">sdiffstore setc seta setb   <span class="comment"># 求差集并且存储，存储到了setc里</span></div><div class="line">sinter seta setb            <span class="comment"># 求交集</span></div><div class="line">sinterstore  setd seta setb <span class="comment"># 将交集存储setd</span></div><div class="line">sunion seta setb            <span class="comment"># 求并集</span></div><div class="line">sunionstore sete seta setb  <span class="comment"># 求并集并存储到sete</span></div><div class="line">sismember seta aaa          <span class="comment"># 判断一个元素是否属于一个集合</span></div><div class="line">srandmember  seta           <span class="comment"># 随机取出一个元素，但不删除</span></div></pre></td></tr></table></figure>
<h2 id="5-zset-数据常用操作"><a href="#5-zset-数据常用操作" class="headerlink" title="5.zset 数据常用操作"></a>5.zset 数据常用操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">zadd zseta 11 123             <span class="comment"># 创建有序集合</span></div><div class="line">zrange zseta 0 -1             <span class="comment"># 显示所有元素，按顺序显示</span></div><div class="line">zrange zseta 0 -1 withscores  <span class="comment"># 可以带上分值</span></div><div class="line">zrem zseta 222                <span class="comment"># 删除指定元素</span></div><div class="line">zrank zseta  222              <span class="comment"># 返回元素的索引值，索引值从0开始，按score正向排序</span></div><div class="line">zrevrank zseta 222            <span class="comment"># 同上，不同的是，按score反序排序</span></div><div class="line">zrevrange  zseta  0 -1        <span class="comment"># 反序显示所有元素，并带分值</span></div><div class="line">zcard zseta                   <span class="comment"># 返回集合中所有元素的个数</span></div><div class="line">zcount  zseta 1 10            <span class="comment"># 返回分值范围1-10的元素个数</span></div><div class="line">zrangebyscore  zseta 1 10     <span class="comment"># 返回分值范围1-10的元素</span></div><div class="line">zremrangebyrank zseta  0 2    <span class="comment"># 删除索引范围0-2的元素，按score正向排序</span></div><div class="line">zremrangebyscore zseta  1 10  <span class="comment"># 删除分值范围1-10的元素</span></div></pre></td></tr></table></figure>
<h2 id="6-键值相关操作"><a href="#6-键值相关操作" class="headerlink" title="6.键值相关操作"></a>6.键值相关操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">keys *                 <span class="comment"># 取出所有key</span></div><div class="line">keys my*               <span class="comment"># 模糊匹配</span></div><div class="line">exists name            <span class="comment"># 有name键 返回1 ，否则返回0；</span></div><div class="line">del  key1              <span class="comment"># 删除一个key，成功返回1 ，否则返回0；</span></div><div class="line">EXPIRE key1 100        <span class="comment"># 设置key1 100s后过期</span></div><div class="line">ttl key                <span class="comment"># 查看键 还有多长时间过期，单位是s,当 key 不存在时，返回 -2 。 当 key 存在但没有设置剩余生存时间时，返回 -1 。 否则，返回 key 的剩余生存时间。</span></div><div class="line">select  0              <span class="comment"># 代表选择当前数据库，默认进入0 数据库</span></div><div class="line">move age 1             <span class="comment"># 把age 移动到1 数据库</span></div><div class="line">persist key1           <span class="comment"># 取消key1的过期时间</span></div><div class="line">randomkey              <span class="comment"># 随机返回一个key</span></div><div class="line">rename oldname newname <span class="comment"># 重命名key</span></div><div class="line"><span class="built_in">type</span> key1              <span class="comment"># 返回键的类型</span></div></pre></td></tr></table></figure>
<h2 id="7-服务相关操作"><a href="#7-服务相关操作" class="headerlink" title="7.服务相关操作"></a>7.服务相关操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">dbsize   <span class="comment"># 返回当前数据库中key的数目</span></div><div class="line">info     <span class="comment"># 返回redis数据库状态信息</span></div><div class="line">flushdb  <span class="comment"># 清空当前数据库中所有的键</span></div><div class="line">flushall <span class="comment"># 清空所有数据库中的所有的key</span></div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/5. php 使用 redis 储存/">php 使用 redis 储存</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<h2 id="1-redis-扩展模块安装"><a href="#1-redis-扩展模块安装" class="headerlink" title="1.redis 扩展模块安装"></a>1.redis 扩展模块安装</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载模块</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@redis ~]<span class="comment"># wget https://codeload.github.com/phpredis/phpredis/zip/develop</span></div><div class="line">[root@redis ~]<span class="comment"># mv develop phpredis.zip</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以下载的时候就重命名</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@redis ~]<span class="comment"># wget https://codeload.github.com/phpredis/phpredis/zip/develop -O phpredis.zip</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@redis ~]<span class="comment"># unzip phpredis.zip </span></div><div class="line">[root@redis ~]<span class="comment"># cd phpredis-develop/</span></div><div class="line">[root@redis phpredis-develop]<span class="comment"># /usr/local/php/bin/phpize </span></div><div class="line">[root@redis phpredis-develop]<span class="comment"># ./configure --with-php-config=/usr/local/php/bin/php-config </span></div><div class="line">[root@redis phpredis-develop]<span class="comment"># make &amp;&amp; make install</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑 /usr/local/php/etc/php.ini </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@redis ~]<span class="comment"># vim /usr/local/php/etc/php.ini</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加一行extension = redis.so</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">extension = redis.so</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E4%BD%BF%E7%94%A8%20redis%20%E5%82%A8%E5%AD%98/01.png?raw=true" alt="01"><figcaption class="figure__caption">01</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;/usr/local/php/bin/php -m |grep redis 检查是否成功</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@redis ~]<span class="comment"># /usr/local/php/bin/php -m |grep redis</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E4%BD%BF%E7%94%A8%20redis%20%E5%82%A8%E5%AD%98/02.png?raw=true" alt="02"><figcaption class="figure__caption">02</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <a href="https://hcldirgit.github.io/2017/08/24/Redis/6.%20PHP-redis%E4%B8%AD%E6%96%87%E6%96%87%E6%A1%A3/" target="_blank" rel="external">PHP-redis中文文档</a></p>
<h2 id="2-redis-实现-session-共享"><a href="#2-redis-实现-session-共享" class="headerlink" title="2. redis 实现 session 共享"></a>2. redis 实现 session 共享</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;php.ini中加入（不建议）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">session.save_handler = <span class="string">"redis"</span></div><div class="line">session.save_path = <span class="string">"tcp://127.0.0.1:6379"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者apache虚拟主机加入</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">php_value session.save_handler <span class="string">"redis"</span></div><div class="line">php_value session.save_path <span class="string">"tcp://127.0.0.1:6379"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E4%BD%BF%E7%94%A8%20redis%20%E5%82%A8%E5%AD%98/03.png?raw=true" alt="03"><figcaption class="figure__caption">03</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者php-fpm.conf对应的pool中加入</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">php_value[session.save_handler] = redis</div><div class="line">php_value[session.save_path] = <span class="string">" tcp://127.0.0.1:6379 "</span></div></pre></td></tr></table></figure>
<h2 id="3-redis-储存-session-测试"><a href="#3-redis-储存-session-测试" class="headerlink" title="3. redis 储存 session 测试"></a>3. redis 储存 session 测试</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@redis ~]<span class="comment"># wget http://study.lishiming.net/.mem_se.txt</span></div><div class="line">[root@redis ~]<span class="comment"># mv .mem_se.txt /usr/local/apache2/htdocs/session.php</span></div><div class="line">[root@redis ~]<span class="comment"># curl localhost/session.php</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E4%BD%BF%E7%94%A8%20redis%20%E5%82%A8%E5%AD%98/04.png?raw=true" alt="04"><figcaption class="figure__caption">04</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出类似于 1489247886<br><br>1489248876<br><br>v2k9k36f309jp5be74uussp3q4</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;连接redis：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@redis phpredis-develop]<span class="comment"># redis-cli </span></div><div class="line">127.0.0.1:6379&gt; keys *</div><div class="line">1) <span class="string">"PHPREDIS_SESSION:v2k9k36f309jp5be74uussp3q4"</span></div><div class="line">2) <span class="string">"PHPREDIS_SESSION:36htag7ctc7ueq60hreubhaho3"</span></div><div class="line">127.0.0.1:6379&gt; get PHPREDIS_SESSION:v2k9k36f309jp5be74uussp3q4</div><div class="line"><span class="string">"TEST|i:1489247886;TEST3|i:1489247886;"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E4%BD%BF%E7%94%A8%20redis%20%E5%82%A8%E5%AD%98/05.png?raw=true" alt="05"><figcaption class="figure__caption">05</figcaption></figure></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/3. Redis主从配置/">Redis主从配置</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<h2 id="1-redis-安装主从"><a href="#1-redis-安装主从" class="headerlink" title="1.redis 安装主从"></a>1.redis 安装主从</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;两台服务器： master （192.168.0.79） 和 slave （192.168.0.78）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;分别按照之前介绍的步骤安装好redis并启动</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;master 配置文件不用动</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;slave 配置文件上加一行  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># vim /usr/local/redis/etc/redis.conf</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Redis%E4%B8%BB%E4%BB%8E%E9%85%8D%E7%BD%AE/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;masterauth  passwd  //如果主上设置了密码，要加这行</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;分别启动master和slave</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果报错可以</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;只需在主上配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">protected-mode   no</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>保护模式关掉即可</strong>。</p>
<h2 id="2-测试主从"><a href="#2-测试主从" class="headerlink" title="2.测试主从"></a>2.测试主从</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;master上：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@redis ~]<span class="comment"># redis-cli</span></div><div class="line">127.0.0.1:6379&gt; <span class="built_in">set</span> k1 v1</div><div class="line">OK</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;slave上：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># redis-cli</span></div><div class="line">127.0.0.1:6379&gt; get k1</div><div class="line"><span class="string">"v1"</span></div></pre></td></tr></table></figure>
<h2 id="3-redis-主从其他相关配置"><a href="#3-redis-主从其他相关配置" class="headerlink" title="3.redis 主从其他相关配置"></a>3.redis 主从其他相关配置</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;slave-read-only yes  #让从只读</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;repl-ping-slave-period 10  #设置slave向master发起ping的频率，每10s发起一次</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;repl-timeout 60 #设置slave ping不通master多少s后就超时</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;repl-disable-tcp-nodelay no #是否开启tcp_nodelay,开启后将会使用更少的带宽，但会有延迟，所以建议关闭</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;repl-backlog-size 1mb #同步队列的长度，backuplog是master的一个缓冲区，主从断开后，master会先把数据写到缓冲区，slave再次连接会从缓冲区中同步数据</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;repl-backlog-ttl 3600  #主从断开后，缓冲区的有效期，默认1小时</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;slave-priority 100 #多个slave是可以设置优先级的，数值越小优先级越高，应用于集群中，支持slave切换为master，优先级最高的才会切换</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;slave的延迟高于10s，那么master就会暂时停止写操作。这两个数值任何一个为0，则关闭该功能，默认第一数值是0。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;min-slaves-max-lag 10</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/27. redis持久化 -- rdb和aof/">redis持久化 -- rdb和aof</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis如果不把数据做持久化，则是把数据存储在内存里面的，所以速度很快。而持久化的目的是为了保证数据安全。redis提供了两种持久化的方案：rdb和appendonly（简称aof）。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;简单讲，rdb形式就是把具体的数据（key以及value）存到到一个文件中；而aof这种是把操作过程以日志的形式存到一个文件里，类似于mysql的bin-log。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;rdb，需要定义dir，也就是数据所存放的目录，其实aof文件也会放到这个目录下面，而dbfilename则定义了rdb文件的名字。配置文件中有一个参数“save”来定义redis按照什么样的规则把内存中的数据刷新到磁盘上的rdb文件里。当然我们也可以用命令“save”或者“bgsave”来手动把内存里面的数据更新到磁盘上。如果想要关闭rdb持久化，可以直接把save参数的值设置为空，即save “”，或者在redis-cli命令行里面执行：config set save “”.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;aof，有一个参数是控制它的开关。“appendonly yes” 只有这个是yes时，才会在dir目录中生成一个aof文件，文件名字默认是appendonly.aof，当然我们可以用appendfilename参数去修改它。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于aof文件，如果我们不做特殊操作，它会无限制增长，以至于把磁盘撑爆。如果业务对redis数据没有那么高的要求的话，可以不用开启aof。另外有一个方法可以实现aof文件的优化，就是执行命令 BGREWRITEAOF，这样就会重写aof文件，最终将aof文件的体积减小。这个过程可能会耗费一定的资源，建议在凌晨执行。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后，再来总结一下。rdb并不会实时把内存数据写到磁盘中，意外断电时会丢一部分数据的，但是aof就安全得多，它几乎是实时记录日志的，通过参数appendfsync控制，默认everysec是一秒记录一次。两种持久化的方式是可以并存的，当数据恢复时，以aof优先。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/26. Redis 简介/">Redis 简介</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis 与其他 key - value 缓存产品有以下三个特点：</p>
<ul>
<li>Redis支持数据的持久化，可以将内存中的数据保存在磁盘中，重启的时候可以再次加载进行使用。</li>
<li>Redis不仅仅支持简单的key-value类型的数据，同时还提供list，set，zset，hash等数据结构的存储。</li>
<li>Redis支持数据的备份，即master-slave模式的数据备份。</li>
</ul>
<h2 id="Redis-优势"><a href="#Redis-优势" class="headerlink" title="Redis 优势"></a>Redis 优势</h2><ul>
<li>性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s 。</li>
<li>丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。</li>
<li>原子 – Redis的所有操作都是原子性的，同时Redis还支持对几个操作全并后的原子性执行。</li>
<li>丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性。</li>
</ul>
<h2 id="Redis与其他key-value存储有什么不同？"><a href="#Redis与其他key-value存储有什么不同？" class="headerlink" title="Redis与其他key-value存储有什么不同？"></a>Redis与其他key-value存储有什么不同？</h2><ul>
<li>Redis有着更为复杂的数据结构并且提供对他们的原子性操作，这是一个不同于其他数据库的进化路径。Redis的数据类型都是基于基本数据结构的同时对程序员透明，无需进行额外的抽象。</li>
<li>Redis运行在内存中但是可以持久化到磁盘，所以在对不同数据集进行高速读写时需要权衡内存，因为数据量不能大于硬件内存。在内存数据库方面的另一个优点是，相比在磁盘上相同的复杂的数据结构，在内存中操作起来非常简单，这样Redis可以做很多内部复杂性很强的事情。同时，在磁盘格式方面他们是紧凑的以追加的方式产生的，因为他们并不需要进行随机访问。</li>
</ul>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/25. Redis 数据类型/">Redis 数据类型</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis支持五种数据类型：string（字符串），hash（哈希），list（列表），set（集合）及zset(sorted set：有序集合)。</p>
<h2 id="String（字符串）"><a href="#String（字符串）" class="headerlink" title="String（字符串）"></a>String（字符串）</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;string是redis最基本的类型，你可以理解成与Memcached一模一样的类型，一个key对应一个value。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;string类型是二进制安全的。意思是redis的string可以包含任何数据。比如jpg图片或者序列化的对象 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;string类型是Redis最基本的数据类型，一个键最大能存储512MB。</p>
<h3 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SET name <span class="string">"runoob"</span></div><div class="line">OK</div><div class="line">redis 127.0.0.1:6379&gt; GET name</div><div class="line"><span class="string">"runoob"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在以上实例中我们使用了 Redis 的 <strong>SET</strong> 和 <strong>GET</strong> 命令。键为 name，对应的值为 <strong>runoob</strong>。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>注意</strong>：一个键最大能存储512MB。</p>
<h2 id="Hash（哈希）"><a href="#Hash（哈希）" class="headerlink" title="Hash（哈希）"></a>Hash（哈希）</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis hash 是一个键值对集合。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis hash是一个string类型的field和value的映射表，hash特别适合用于存储对 象。</p>
<h3 id="实例-1"><a href="#实例-1" class="headerlink" title="实例"></a>实例</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; HMSET user:1 username runoob password runoob points 200</div><div class="line">OK</div><div class="line">127.0.0.1:6379&gt; HGETALL user:1</div><div class="line">1) <span class="string">"username"</span></div><div class="line">2) <span class="string">"runoob"</span></div><div class="line">3) <span class="string">"password"</span></div><div class="line">4) <span class="string">"runoob"</span></div><div class="line">5) <span class="string">"points"</span></div><div class="line">6) <span class="string">"200"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上实例中 hash 数据类型存储了包含用户脚本信息的用户对象。 实例中我们使用了   Redis <strong>HMSET</strong>, <strong>HGETALL</strong> 命令，<strong>user:1</strong>为键值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;每个 hash 可以存储 232 -1 键值对（40多亿）。</p>
<h2 id="List（列表）"><a href="#List（列表）" class="headerlink" title="List（列表）"></a>List（列表）</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis 列表是简单的字符串列表，按照插入顺序排序。你可以添加一个元素到列表的头部（左边）或者尾部（右边）。</p>
<h3 id="实例-2"><a href="#实例-2" class="headerlink" title="实例"></a>实例</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; lpush runoob redis</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; lpush runoob mongodb</div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; lpush runoob rabitmq</div><div class="line">(<span class="built_in">integer</span>) 3</div><div class="line">redis 127.0.0.1:6379&gt; lrange runoob 0 10</div><div class="line">1) <span class="string">"rabitmq"</span></div><div class="line">2) <span class="string">"mongodb"</span></div><div class="line">3) <span class="string">"redis"</span></div><div class="line">redis 127.0.0.1:6379&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列表最多可存储 232 - 1 元素 (4294967295, 每个列表可存储40多亿)。</p>
<h2 id="Set（集合）"><a href="#Set（集合）" class="headerlink" title="Set（集合）"></a>Set（集合）</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis的Set是string类型的无序集合。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;集合是通过哈希表实现的，所以添加，删除，查找的复杂度都是O(1)。</p>
<h3 id="sadd-命令"><a href="#sadd-命令" class="headerlink" title="sadd 命令"></a>sadd 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加一个string元素到,key对应的set集合中，成功返回1,如果元素已经在集合中返回 0,key对应的set不存在返回错误。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sadd key member</div></pre></td></tr></table></figure>
<h3 id="实例-3"><a href="#实例-3" class="headerlink" title="实例"></a>实例</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; sadd runoob redis</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; sadd runoob mongodb</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; sadd runoob rabitmq</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; sadd runoob rabitmq</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis 127.0.0.1:6379&gt; smembers runoob</div><div class="line"> </div><div class="line">1) <span class="string">"rabitmq"</span></div><div class="line">2) <span class="string">"mongodb"</span></div><div class="line">3) <span class="string">"redis"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>注意</strong>：以上实例中 rabitmq 添加了两次，但根据集合内元素的唯一性，第二次插入的元素将被忽略。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;集合中最大的成员数为 232 - 1(4294967295, 每个集合可存储40多亿个成员)。</p>
<h2 id="zset-sorted-set：有序集合"><a href="#zset-sorted-set：有序集合" class="headerlink" title="zset(sorted set：有序集合)"></a>zset(sorted set：有序集合)</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis zset 和 set 一样也是string类型元素的集合,且不允许重复的成员。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;zset的成员是唯一的,但分数(score)却可以重复。</p>
<h3 id="zadd-命令"><a href="#zadd-命令" class="headerlink" title="zadd 命令"></a>zadd 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加元素到集合，元素在集合中存在则更新对应score</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">zadd key score member</div></pre></td></tr></table></figure>
<h3 id="实例-4"><a href="#实例-4" class="headerlink" title="实例"></a>实例</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; zadd runoob 0 redis</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; zadd runoob 0 mongodb</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; zadd runoob 0 rabitmq</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; zadd runoob 0 rabitmq</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis 127.0.0.1:6379&gt; ZRANGEBYSCORE runoob 0 1000</div><div class="line"> </div><div class="line">1) <span class="string">"redis"</span></div><div class="line">2) <span class="string">"mongodb"</span></div><div class="line">3) <span class="string">"rabitmq"</span></div></pre></td></tr></table></figure>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/17">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
